Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
05-17-2010, 06:14 PM
|
#1
|
Member
Registered: Dec 2008
Location: Norway
Distribution: Debian
Posts: 43
Rep:
|
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.
|
|
|
05-17-2010, 06:16 PM
|
#2
|
Senior Member
Registered: May 2005
Posts: 4,481
|
Quote:
Originally Posted by Mogget
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.
|
|
|
05-17-2010, 06:23 PM
|
#3
|
Member
Registered: Dec 2008
Location: Norway
Distribution: Debian
Posts: 43
Original Poster
Rep:
|
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?
|
|
|
05-17-2010, 06:52 PM
|
#4
|
Senior Member
Registered: May 2005
Posts: 4,481
|
Quote:
Originally Posted by Mogget
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" ?
|
|
|
05-17-2010, 08:48 PM
|
#5
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078
Rep: 
|
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
|
|
|
05-17-2010, 09:05 PM
|
#6
|
Senior Member
Registered: May 2005
Posts: 4,481
|
Quote:
Originally Posted by ta0kira
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.
|
|
|
05-18-2010, 12:25 AM
|
#7
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078
Rep: 
|
Quote:
Originally Posted by Sergei Steshenko
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
|
|
|
05-18-2010, 02:11 AM
|
#8
|
Member
Registered: May 2010
Posts: 136
Rep:
|
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 02:17 AM.
|
|
|
05-18-2010, 04:32 AM
|
#9
|
Member
Registered: Dec 2008
Location: Norway
Distribution: Debian
Posts: 43
Original Poster
Rep:
|
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.
|
|
|
05-18-2010, 05:48 AM
|
#10
|
Member
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811
Rep: 
|
Quote:
Originally Posted by Mogget
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:18 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|