用 C 语言进行 UDP 广播需要什么?
我正在从事广播工作,但我什至在让这件事发挥作用方面都非常失败。我知道我必须执行setsockopt() 调用,但是在此之前需要什么才能确保广播到达网络上的每个盒子?我依稀记得一些关于补充网络地址之类的事情。我很感激你的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要使用setsockopt()。发送到特殊地址 255.255.255.255 的 UDP 数据包将被发送到本地网络上的所有地址。
如果您想要广播的网络不是本地的,您需要使用该网络的广播地址(根据 ivymike 的注释),按照惯例,该地址通常(但并非总是)子网中的最后一个 IP。
请记住,广播地址用于许多 DOS 攻击,并且很可能被过滤。
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.
当涉及到 Windows 世界时,我不太确定接受的答案是否正确。
我有一个 UDPSocket 类,其灵感来自于 这个答案。它已经使用了一段时间并且运行良好。但我只是尝试使用它来创建一个向
255.255.255.255
广播的WakeOnLan()
函数,并且sendto()
调用失败,并显示Windows 套接字错误 10013 - 权限被拒绝(至少在我的 Windows 7 系统上测试过)。因此,我根据 Remy Lebeau 对 这个问题,紧接在
socket()
调用之后。这启用了广播,并且是生成我的WakeOnLan()
函数时缺少的链接。我并不是说这是“答案”,但对于评论来说太长了,社区维基可能希望在这个答案中编辑一个更普遍的事实,以造福那些发现已接受的答案不足的人。
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 aWakeOnLan()
function that broadcasts to255.255.255.255
, and thesendto()
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 asetsockopt()
call based on Remy Lebeau's answer to this question, placed immediately after thesocket()
call. This enabled broadcasting, and was the missing link in producing myWakeOnLan()
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.