开发者

c++ send data to multiple UDP sockets

开发者 https://www.devze.com 2022-12-09 02:38 出处:网络
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 work

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 somet开发者_如何学JAVAhing 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.


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.


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消