How to broadcast Message using UDP sockets locally?

The server should not be bound to an address you get from getaddrinfo, instead it should be bound to 127.255.255.255 (for the loopback interface).

For a ready-made example of broadcast server/client see http://www.ccplusplus.com/2011/09/udp-broadcast-client-server-example.html


Unix domain sockets don't support multi-/broadcasting.

You can broadcast on the local interface 127.0.0.1.


While the original question doesn't explicitly say so, I believe the original asker wanted to 'broadcast' to multiple applications running on the same operating-system instance (same computer to old timers).

This is supported by the use of 'SO_REUSEADDR' in the listener example, and followup comments by Yuvi, and finally a suggestion to use IP multicast.

The original question should be clarified.

I believe packet distribution with multiple-binders on a single UDP port varies between operating systems when using SO_REUSEADDR. My experience on recent Windows, is that a single 'binder' is exclusively given all packets until she releases her bind, at which time, another binder is chosen and presented all received packets, until she releases, and so on...

This apparently differs from recent Linux kernels, as explained in this link: https://stackoverflow.com/a/14388707/86375 That page appears to claim Linux will round-robin receives packets between multiple binders.

The end-result, if you hope to send-to-many using a single sent-datagrams as the original poster did, and you attempt to use IP unicast, not IP multicast, you may be disappointed. (My experience, and the link above show you can multi-bind, but that doesn't imply multi-delivery of received datagrams, neither on Linux or Windows)

The original poster should have tried using multicast.

Tags:

Linux

C

Sockets

Ipc