开发者

What is required to make a UDP Broadcast in C?

开发者 https://www.devze.com 2023-01-20 03:27 出处:网络
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

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 complementin开发者_高级运维g the network address or something like that. I appreciate the help.


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.


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.

0

精彩评论

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