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. |
Quote:
|
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? |
Quote:
|
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 |
Quote:
|
Quote:
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"? |
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.
|
Quote:
|
All times are GMT -5. The time now is 05:30 PM. |