views:

331

answers:

4

Hi all!

I got a c++ non-blocking server socket, with all the clients stored in a std::map structure.

I can call the send() method for each clientObject to send something to the connected client and that works pretty good already.

But for sending a message to all (broadcast?) i wanna know: there is something better than do a for/loop with all the clients and call to ClientObject->send("foo") each iteration?

Or should i just try having a peek on multicast sockets?

Thanks in advance. Rag.

+1  A: 

Multicast would be much preferable... as long as you are talking about local nodes i.e. within the "broadcast/multicast" domain on the LAN.

Of course there are multicast distribution protocols for wider dispersion of such messages but they are seldom used and, depending on your specific case, you could/couldn't reliability depend on such facility.

The use of Multicast translates to lots of savings from a sender point of view: only one send operation needs to occur instead of n*send.

jldupont
+1  A: 

Multicast is only an option if you're communicating over a LAN. It won't work over the Internet.

What you may want to do here is to demultiplex the sockets using asynchronous I/O. This allows you to send data to multiple sockets at the same time, and use asynchronous event handlers to deal with each transmission.

I would recommend looking into Boost ASIO for a portable way to do this. You can also use OS specific system calls, (such as poll/select on UNIX or epoll on Linux) to do this, but it is a lot more complicated.

Charles Salvia
Multicast works over the Internet, given some restrictions and some proper router configuration that is usually forgotten by most network admins. Just one example, you can listen to high-quality radio or watch high-quality TV from BBC over multicast: http://www.bbc.co.uk/multicast/
Juliano
A: 

You'd better off to do udp unicast to each host unless you have those very expensive switches. Yes, broadcast/multicast can actually be slower for most switches that have much wimpier CPU than your pcs. Doing anything other than simple forwarding would slow them down tremendously.

Do a benchmark to find out.

obecalp
A: 

Asynch socket programming is definitely the way to go! :)

bobber205