LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   multiclient communicaion (https://www.linuxquestions.org/questions/programming-9/multiclient-communicaion-643432/)

sweetytweety 05-20-2008 02:24 AM

multiclient communicaion
 
hi,
i ve done a multiclient socket program in which n of clients can communicte to server(client send and receive message from server).what i what is i need two clients should communicate through the server. How to do that?

MS3FGX 05-20-2008 02:30 AM

Perhaps this would be better in Programming?

b0uncer 05-20-2008 04:38 AM

I'm working on such a program myself right now -- learning C++ that way -- and am halfway there..basically your server should listen to the sockets and when something comes out from one of them, it should send it to the rest of the sockets, right?

Tinkster 05-20-2008 01:21 PM

Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.

theNbomr 05-20-2008 01:54 PM

An HTTP server, among many other examples, provides the essence of your objective. Perhaps studying the code from one of the several 'mini web servers' would be instructive. Beej's Guide to Network Programming should also be helpful.
The general solution to the problem is that on receipt of a new connection, a child process is forked, and the child handles all communication with that specific client connection until the connection closes.
--- rod.

Wim Sturkenboom 05-20-2008 11:35 PM

I think that the original question is misunderstood. My guess is that sweetysweety looks for a way to let 2 clients communicate with each other through the server.

If so:

In the simplest scenario, the server will send received messages to all clients. In that case I would let the server keep track of all open client sockets (store in an e.g array). When data is received, you can iterate through the array and send the data to the sockets in that array.

It becomes a bit more complex if you only want to communicate between a variable number of clients. In that case the server has to keep track of the client sockets that are 'linked'. I will probably store the 'linked' sockets in an array of structs (containing two sockets). When data is received, the server will iterate through the array to find the 'linked' sockets and send the data to those sockets.

theNbomr 05-21-2008 09:41 AM

Wim, I think you may be right. I see a couple of devilish details to cope with in the objective. How does one manage connections where the desired client peer has not yet connected? Do you need to relay connection management data to each client (eg: what, if any, other clients are connected)? How do you interleave data received concurrently from multiple clients (assuming all clients get copies of data sent from other clients)? Does the server need to distribute 'channel' directory info if there is a possibility for peers to communicate in distinct groups?
It seems to me that the most well known model for this sort of communication would be an IRC server. Although I don't normally use those much at all, I believe they do what you are suggesting. Are the servers for those generally available in open-source format?
Overall, an intriguing application. I hope someone can post a definitive example of how this might be done.
--- rod.


All times are GMT -5. The time now is 07:55 AM.