LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Sockets with linux, how to send same text string to all connected clients c/c++ (https://www.linuxquestions.org/questions/programming-9/sockets-with-linux-how-to-send-same-text-string-to-all-connected-clients-c-c-808438/)

Mogget 05-17-2010 05:14 PM

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.

Sergei Steshenko 05-17-2010 05:16 PM

Quote:

Originally Posted by Mogget (Post 3971977)
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.

You might think of creating a separate thread per socket and sending in parallel.

Mogget 05-17-2010 05:23 PM

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?

Sergei Steshenko 05-17-2010 05:52 PM

Quote:

Originally Posted by Mogget (Post 3971984)
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?

What is "they" ?

ta0kira 05-17-2010 07:48 PM

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

Sergei Steshenko 05-17-2010 08:05 PM

Quote:

Originally Posted by ta0kira (Post 3972114)
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.

ta0kira 05-17-2010 11:25 PM

Quote:

Originally Posted by Sergei Steshenko (Post 3972130)
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

posixculprit 05-18-2010 01:11 AM

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"?

Mogget 05-18-2010 03:32 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.

wje_lq 05-18-2010 04:48 AM

Quote:

Originally Posted by Mogget (Post 3972493)
i haven't gotten that far in the book i'm reading.

Um, Mogget, may I ask which book you're reading? This question might be more important than it initially sounds.


All times are GMT -5. The time now is 05:30 PM.