ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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?
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.