LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 05-17-2010, 05:14 PM   #1
Mogget
Member
 
Registered: Dec 2008
Location: Norway
Distribution: Debian
Posts: 43

Rep: Reputation: 15
Cool 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.
 
Old 05-17-2010, 05:16 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Mogget View Post
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.
 
Old 05-17-2010, 05:23 PM   #3
Mogget
Member
 
Registered: Dec 2008
Location: Norway
Distribution: Debian
Posts: 43

Original Poster
Rep: Reputation: 15
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?
 
Old 05-17-2010, 05:52 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Mogget View Post
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" ?
 
Old 05-17-2010, 07:48 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 05-17-2010, 08:05 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ta0kira View Post
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.
 
Old 05-17-2010, 11:25 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Sergei Steshenko View Post
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
 
Old 05-18-2010, 01:11 AM   #8
posixculprit
Member
 
Registered: May 2010
Posts: 136

Rep: Reputation: 42
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.
 
Old 05-18-2010, 03:32 AM   #9
Mogget
Member
 
Registered: Dec 2008
Location: Norway
Distribution: Debian
Posts: 43

Original Poster
Rep: Reputation: 15
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.
 
Old 05-18-2010, 04:48 AM   #10
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 178Reputation: 178
Quote:
Originally Posted by Mogget View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sockets: Send string to server introuble Programming 13 07-02-2006 06:38 PM
Linux clients sharing a common printer connected to a windows box comox Linux - Networking 1 10-24-2005 06:50 PM
how can i send a file using linux sockets?? crapodino Programming 2 10-09-2005 11:17 PM
Command to check sockets/connected ip Cyric Slackware 5 11-21-2004 06:26 AM
windows clients not being able to send mail through linux server kpg Linux - Networking 3 02-12-2004 01:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration