tags:

views:

64

answers:

5

I have been searching Google for a while, but the problem I am running into is I am not exactly sure what it is I need to be searching for. (Searching for PHP C++ communication doesn't seem to be what I need) I am basically developing a c++ plugin for a game server, and I would like to create a web interface that can pass/pull data to and from the C++ plugin. The game already uses an RCON port for remote administrative access, but I stumbled across a header for the Network interface they use, so I assume I could use this.

My problem is I am not very familiar with using sockets. I assume I will basically need to open a socket in C++ and leave it listening, and then in the PHP, connect to that socket, pass the data, and close it.

Here is the interface... http://www.ampaste.net/m2f6b6dbc

I am mostly just going to be pulling information like current list of connected players, names, and scores. And passing commands to restart the server, shut it down, etc.

Any help would be great, thanks!

+1  A: 

I'd dare to recommend to use some standard means of distributed components communication, for example, XML RPC. There are libraries for both PHP and C++: http://en.wikipedia.org/wiki/XML-RPC#Implementations

This approach will keep you from reinventing the wheel during communication protocol implementation, and will make further maintenance cheaper.

Kel
A: 

What it appears you want to google is C++ client / server. There are two approaches I could suggest here.

First, would be to make a very basic HTTP protocol server so that your php script can simply go to http://yourip/ and send your commands through the POST variables. You can find an example of a C++ Web Server at: http://stackoverflow.com/questions/175507/c-c-web-server-library

The second approach which allows a lot more flexibility is make up your own basic protocol and use PHP's SOCKETS to connect to the server and send commands. You can find an example of a C++ client / server application at http://www.codeproject.com/KB/IP/client_server_socket.aspx. Keep in mind, for the C++ end, you are only concerned about the Server part. You can find a basic PING client in PHP, using sockets, at the following URL: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=8&txtCodeId=1786. There are also classes out there to handle most of the protocol part, though I am not aware of any that work for both languages.

Please note I have not tested any of the codes I linked to. I simply found them on google.

Anthony Greco
A: 

A good place to start would be http://php.net/manual/en/book.sockets.php.

Basically, you're going to create another remote administration port and method for PHP to connect. Naturally, if your going to only be accepting web communication from one IP, that's a good way to secure it (check and allow access to only the one IP which will connect). However, you will need the C++ server to listen on a (secure?) port and have PHP connect to it (as long as host allows it).

So overall, if you already have a server running, this should be simple from the C++ side. All you need to do from the PHP side is really research connecting to different servers and passing information along (which PHP is more than capable of doing efficiently)

But, this is obviously an alternative to the poster up 2. I personally enjoy (in many cases) "reinventing the wheel" so to speak as to be able to manage my own work. But of course, that is not always efficient by cost or otherwise.

Good luck!

RageD
A: 

You could try Thrift. It was written by the engineers at Facebook, and it's now an Apache project.

Thrift is a software framework for scalable cross-language services development. It combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk, and OCaml.

Link: http://incubator.apache.org/thrift/

In a nutshell it does exactly what you're trying to do. It makes it easy for different languages to communicate with each other. Rather than trying to come up with some socket based protocol for communication, you can call a function in PHP like this:

$game->getScores();

And it automatically plugs into a function named getScores in your C/C++ program. The only drawback is it can be a bit of a pain to configure correctly.

mellowsoon
A: 

I assume I will basically need to open a socket in C++ and leave it listening

err, yes, that's the description I'd give to my 12 year-old daughter - but if you're going to have more than one client connecting its a bit more involved. Especially if you are bolting the code onto an existing server. So have a read of the socket programming FAQ.

You do need to define a protocol of how data will be represented when travelling across the socket. THere are lots of 'standard methods - but sometimes things like CORBA / SOAP etc can just be overkill and more effort than starting from scratch.

If you are bolting code ontp an existing server, life will be a lot simpler if you use the current socket and extend the protocol if necessary.

There are 3 models for writing a socket server - the code snippet you provided does not seem to include details of which you are currently working with:

  1. forking server (may split threads rather than processes)
  2. single-threaded server
  3. socketless server

forking server

  • An instance of the server is started (call it p1), calling setsid()
  • p1 starts listening on the relevant socket
  • a client tries to connect
  • p1 forks to create p2
  • p2 then accepts the connection and starts conversing with the client
  • p1 continues to listen for further connections
  • p2 exits when the connection closes

There are variations of this - p2 may accept further connections, p1 might fork prior to a connection coming in)

single-threaded

  • An instance of the server is started, calling setsid()
  • it starts listening for a connection, and creates an array of the sockets in use (including the initial one)
  • socket_select() is used to identify activity from any of the sockets
  • when a client connects, the connection is accepted and added to an array of connections
  • whenever socket_select() returns activity on one of the sockets, the server generaets an appropriate response / closes the socket / binds the new connection

socketless server

  • some process (e.g. inetd) handles all the socket stuff
  • when a client connects, this other server starts an instance of your program and binds the socket I/O to the STDIN/STDOUT of your program
  • when your program exits, the other process closes the socket (if its still open) and handles the clean up (e.g. if it is implemented as a forking server, then the spawned process may end)
symcbean