ad hoc 网络中的 UDP 套接字 (Ubuntu 9.10)

发布于 2024-09-03 12:55:42 字数 1039 浏览 5 评论 0原文

我在 Ubuntu 9.10 中使用 BSD 套接字通过广播发送 UDP 数据包,代码如下:

        sock_fd = socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
        //sock_fd=socket(AF_INET,SOCK_DGRAM,0);
        receiver_addr.sin_family = PF_INET;
        //does not send with broadcast in ad hoc
        receiver_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
        inet_aton("169.254.255.255",&receiver_addr.sin_addr);
        receiver_addr.sin_port = htons(port);
        int broadcast = 1;
           // this call is what allows broadcast packets to be sent:
           if (setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST, &broadcast,
               sizeof broadcast) == -1) {
               perror("setsockopt (SO_BROADCAST)");
               exit(1);
           }
        ret=sendto(sock_fd, packet, size, 0,(struct sockaddr*)&receiver_addr,sizeof(receiver_addr));

请注意,这不是全部代码,只是一个想法。

如果我连接到基础设施无线网络,程序会使用 INADDR_BROADCAST 发送所有数据。但是,如果我的笔记本电脑连接到临时网络,它可以接收所有数据,但不能发送数据。我已经使用169.254.255.255广播地址解决了问题,但我想知道发生了什么。

先感谢您!

I am using BSD sockets in Ubuntu 9.10 to send UDP packets in broadcast with the following code:

        sock_fd = socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
        //sock_fd=socket(AF_INET,SOCK_DGRAM,0);
        receiver_addr.sin_family = PF_INET;
        //does not send with broadcast in ad hoc
        receiver_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
        inet_aton("169.254.255.255",&receiver_addr.sin_addr);
        receiver_addr.sin_port = htons(port);
        int broadcast = 1;
           // this call is what allows broadcast packets to be sent:
           if (setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST, &broadcast,
               sizeof broadcast) == -1) {
               perror("setsockopt (SO_BROADCAST)");
               exit(1);
           }
        ret=sendto(sock_fd, packet, size, 0,(struct sockaddr*)&receiver_addr,sizeof(receiver_addr));

Note that is not all the code, it is only to have an idea.

The program sends all the data with INADDR_BROADCAST if I am connected to an infrastructure wireless network. However, if my laptop is connected to an ad-hoc network, it is able to receive all the data, but not to send it. I have solved the problem using the 169.254.255.255 broadcast address, but I would like to know what is going on.

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

诗酒趁年少 2024-09-10 12:55:42

您是否有多个适配器(即有线以太网、WLAN)?

当您发送到有限广播地址 (INADDR_BROADCAST) 时,您的 IP 堆栈使用第一个适配器并发送帧。您无法控制选择特定适配器。当 IP 堆栈发送到找到的第一个适配器时,它就完成了它的工作。如果路由器不阻止的话,该帧将在整个互联网上泛滥。这就是默认情况下 UDP 广播不可路由的原因。 (DHCP 中继中实现的情况存在例外。)

当您需要在特定网络上发送 UDP 帧时,您需要网络广播地址。当您在 AUTOIP 网络上发送到 169.254.255.255 时,您已经这样做了。

setsocket(SOL_SOCKET, SO_BROADCAST) 只是将套接字配置为允许发送广播。

Do you have more than one adapter (i.e. wired ethernet, WLAN)?

When you send to the limited broadcast address (INADDR_BROADCAST) your IP stack uses the first adapter and sends the frame. You dont have any control about choosing a specific adapter. The IP stack has done it's job, when it sends to the first adapter found. The frame would be flooded over the whole Internet if this would not be prevented by routers. That's why UDP broadcast are not routable by default. (Exceptions exist as implemented in DHCP relays.)

When you need to send a UDP frame on a specific network, you need the network broadcast address. You did this already, when you send to 169.254.255.255 on a AUTOIP net.

The setsocket(SOL_SOCKET, SO_BROADCAST) just configures the socket to allow sending broadcast at all.

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