用 C 语言进行 UDP 广播需要什么?

发布于 2024-09-27 19:39:50 字数 119 浏览 2 评论 0原文

我正在从事广播工作,但我什至在让这件事发挥作用方面都非常失败。我知道我必须执行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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傾城如夢未必闌珊 2024-10-04 19:39:50

您不需要使用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.

一绘本一梦想 2024-10-04 19:39:50

当涉及到 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文