I worked on a complex server-client system in C, and I'm not sure how to implement socket communication.
In a nutshell, a system is a server application that communicates with a database and uses a UNIX socket to communicate with one or more child processes created using fork() . The goal of the children is to launch game servers. The process of starting the game server is as follows:
- The server / "manager" identifies the game server in the database to be created. (Assume the database connection is already sorted.)
- The manager launches a child element ("game controller").
- The game controller installs two pairs of pipes, then forks, replacing its child stdin with a channel, and stdout and stderr with another.
- Then the child controller of the game runs
execlp() to run the executable file of the real server.
My experience with sockets is pretty minimal. I used select() in a server application before โmultipleโ multiple clients, as evidenced by a simple example in the GNU C documentation here .
I now have a new challenge, since the system should be able to do more: the manager should be able to arbitrarily send commands to the children of the game controller (which he will detect by periodically checking the database) and receive answers, but also expect arbitrary commands / errors from them and send responses back.
So, I need some kind of "context" system, where sockets make sense only among themselves. In other words, when a command is sent from the dispatcher to the game controller, each side must know who is asking and knows what the answer is (and therefore which team is the answer).
Because select() is only useful to know when we have incoming data, and the thread must block it, do I need another thread that sends data and receives responses? Will this require each game controller, although technically a โclientโ, to use a listening socket and also use select() ?
Hope I explained the system and the problem briefly; If necessary, add additional information. Thanks!
Doddy source share