Communicating clients that are connected to the same server

This is a discussion on Communicating clients that are connected to the same server within the Networking/Device Communication forums, part of the General Programming Boards category; First of all, I'm using Linux. Well, the problem is that I have an average basic server (like the one ...

Communicating clients that are connected to the same server

First of all, I'm using Linux. Well, the problem is that I have an average basic server (like the one you can get on the beej tutorial). I see that when the server receives a connection from a client, it uses fork() to generate a new process to handle the client's connection. And that repeats when it receives other client connections. The question is: how can I make those clients, that are in different processes, communicate with each other. Take as an example a chat. Two clients connect to the server, and the server generates a process for each client; but how can the clients exchange information with each other in order to be able to chat?

Well, that's my question, hope I was clear. If there's something you don't understand about my question, please, don't hesitate to ask. Any help will be very appreciated.

You just need to set up some mechanism where one connected user can send a message and specify a user that it goes to. Your server needs to figure out which process that user is connected to, and then send that process the message, along with specifying the user it came from.

Set up a pipe from the server to each client, and just maintain a table of which pipe goes to which user (or use named pipes, and a consistent naming scheme based on the user).

If you don't mind using a thread for each client model, then that is a possibility. I've always found that the easiest model to use when dealing with multiple clients. In each thread receieve all the clients bytes and parse them into something meaningful, and report back to some main thread which will in turn respond to the proper client(s).

Thanks to both of you for your answers. Regarding to the use of threads, I'd like to avoid it if possible. I want to do it in a more elegant way.
Right now I'm reading a lot about process and trying a lot of different stuff without success, trying to use dup2(), execl() and nothing seems to work. But at least I'm understanding a little bit more what I want to do: I want to communicate to CHILD processes. Because each time the server receives a new connection it creates a new child process with fork() and I want to communicate them.

Nevertheless, I'm completely clueless about how to do it. So any further help will be appreciated. I'd be very glad if you can also post some code.

Thanks to both of you for your answers. Regarding to the use of threads, I'd like to avoid it if possible. I want to do it in a more elegant way.
Right now I'm reading a lot about process and trying a lot of different stuff without success, trying to use dup2(), execl() and nothing seems to work. But at least I'm understanding a little bit more what I want to do: I want to communicate to CHILD processes. Because each time the server receives a new connection it creates a new child process with fork() and I want to communicate them.

Nevertheless, I'm completely clueless about how to do it. So any further help will be appreciated. I'd be very glad if you can also post some code.

Thanks in advance.

Sure, if you call elegant more difficult, and computationally much slower. Check out beej's IPC guide for more info on IPC.

I would just use a "pass it on" protocol. You append a couple of characters to all messages and the server interprets that, eg, "CALL1:" and the server will forward this on to the appropriate party. Since that initial signal is appended (and removed) by your program, the users never see or have to deal with it.

That is how an actual chat client works. You do not send a message directly to someone else, you send a message to the server with a wrapper added by the client, and the server uses that wrapper to relay your message.