[SOLVED] Sockets with linux, how to send same text string to all connected clients c/c++
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.
Sockets with linux, how to send same text string to all connected clients c/c++
Hello.
I'm programming a simple Telnet IRC-like server using sockets in linux.
It's all for educational purposes.
I need to be able to send one text string to all connected clients. So far i've been thinking about making an array of socket file descriptions for the clients and then using a loop to send the string one by one to each client in the array. It will probably work, but it feels uneffective and time consuming.
I've been reading up on multicast and broadcast but it doesn't seem like i can use that for my purposes.
I want sugestions on how to do this in terms of algorithm and syntax commands i can use to achieve these things.
Thank you in advance for any ideas/sugestions you may have.
I'm programming a simple Telnet IRC-like server using sockets in linux.
It's all for educational purposes.
I need to be able to send one text string to all connected clients. So far i've been thinking about making an array of socket file descriptions for the clients and then using a loop to send the string one by one to each client in the array. It will probably work, but it feels uneffective and time consuming.
I've been reading up on multicast and broadcast but it doesn't seem like i can use that for my purposes.
I want sugestions on how to do this in terms of algorithm and syntax commands i can use to achieve these things.
Thank you in advance for any ideas/sugestions you may have.
You might think of creating a separate thread per socket and sending in parallel.
When a user connects to the server now they instantly get fork()ed. Is it possible to parallell send the string even though i don't use threads?
And could you give me a very basic idea of how to do so?
When a user connects to the server now they instantly get fork()ed. Is it possible to parallell send the string even though i don't use threads?
And could you give me a very basic idea of how to do so?
You shouldn't worry about parallel sending because you can't count on parallel receiving due to differences in the network dynamics with each client. What's probably more important is the order in which the messages are sent. You might start a message queue (mq_open) for each fork, send the broadcast message to the queue for each fork in a loop, then check the queue in each fork each send iteration. You might need to fine-tune the process synchronization using mutexes or semaphores to make sure messages are sent in the right order.
Kevin Barry
You shouldn't worry about parallel sending because you can't count on parallel receiving due to differences in the network dynamics with each client. What's probably more important is the order in which the messages are sent. You might start a message queue (mq_open) for each fork, send the broadcast message to the queue for each fork in a loop, then check the queue in each fork each send iteration. You might need to fine-tune the process synchronization using mutexes or semaphores to make sure messages are sent in the right order.
Kevin Barry
In order to avoid sending messages to the forked instances I suggested threads in the first place.
In order to avoid sending messages to the forked instances I suggested threads in the first place.
Is there a shared-memory counterpart to pthread conditions (as semaphore is to mutex)? If so, I would have suggested that in conjunction with mmap.
Kevin Barry
Threads or no threads a number of problems appear. I suppose that whenever a client sends a message the message must be sent to "everybody". However: how do you store a received message? When do you dispose of it (i.e. how do you determine that "everyone" has received it and thus it is no longer needed and should be eliminated)? When a new client connects, it should only receive messages which arrive in the system from that point on, rather than receive all the messages that are in the system at that moment. Also, you should probably send the messages in the order in which they were received by the system (this isn't necesserarly the same as the order in which they were sent, but it's probably the best you can do).
P.S.: What do you mean by "Telnet IRC-like"?
Last edited by posixculprit; 05-18-2010 at 01:17 AM.
Thank you everybody, your input has been great. I might switch over to threads. The only reason i'm not using it is because i haven't gotten that far in the book i'm reading. Also the problem wasn't how to get the strings to every user but how to do so efficiently if an efficient way exist. Anyway you have been alot of help. Thank you all again.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.