I'm working on broadcast, and I'm failing pretty badly at even getting the thing to work. I know I have to do the setsockopt() call, but what is needed before that to ensure that the broadcast will go to every box on the network? I vaguely remember something about complementing the network address or something like that. I appreciate the help.
-
2Post your full source code. Read the man pages. – Dave Jarvis Oct 17 '10 at 18:00
-
1Microsoft's lawyers called. They said something about evantravers infringing their patent on bringing local networks to a crawl. – R.. GitHub STOP HELPING ICE Oct 17 '10 at 18:03
-
You will have to direct your broadcast at the broadcast address of the subnet you want to broadcast to. For more info: http://beej.us/guide/bgnet/output/html/multipage/advanced.html#broadcast – ivymike Oct 17 '10 at 18:03
2 Answers
You don't need to use setsockopt(). A UDP packet sent to the special address 255.255.255.255 will be sent to all addresses on the local network.
If the network you want to broadcast to is not local you need to use the broadcast address of that network (as per ivymike's comment), which by convention is normally (but not always) the last IP in the subnet.
Bear in mind that broadcast addresses are used in a number of DOS attacks and are likely to be filtered.

- 885
- 2
- 9
- 16
I'm not so sure the accepted answer is correct when it comes to the Windows world.
I have a UDPSocket
class that is inspired by this answer. It has been in use for quite a while and functioning fine. But I just tried to use it to make a WakeOnLan()
function that broadcasts to 255.255.255.255
, and the sendto()
call failed with Windows Socket Error 10013 - Permission Denied (at least as tested on my Windows 7 system).
So I extended my UDPSocket
constructor with a setsockopt()
call based on Remy Lebeau's answer to this question, placed immediately after the socket()
call. This enabled broadcasting, and was the missing link in producing my WakeOnLan()
function.
I'm not suggesting that this is "the answer", but it's too long for a comment, and the community wiki might like to edit a more general truth into this answer for the benefit of those who find the accepted answer falls short.