C++ 接收UDP广播

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12515005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:20:36  来源:igfitidea点击:

Receiving UDP broadcast

c++udp

提问by Blindy

I have to receive an UDP broadcast (in Ubuntu if that makes any difference). Using Wireshark, I can see the packet being sent from the server machine, and I can see it being received by my client machine, but my program is completely oblivious. This is what I have:

我必须接收 UDP 广播(在 Ubuntu 中,如果这有什么不同的话)。使用 Wireshark,我可以看到从服务器机器发送的数据包,并且我可以看到它被我的客户端机器接收,但是我的程序完全没有注意到。这就是我所拥有的:

sockaddr_in si_me, si_other;
int s;
assert((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))!=-1);
int port=6000;
int broadcast=1;

setsockopt(s, SOL_SOCKET, SO_BROADCAST,
            &broadcast, sizeof broadcast);

memset(&si_me, 0, sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(port);
si_me.sin_addr.s_addr = INADDR_ANY;

assert(::bind(s, (sockaddr *)&si_me, sizeof(sockaddr))!=-1);

while(1)
{
    char buf[10000];
    unsigned slen=sizeof(sockaddr);
    recvfrom(s, buf, sizeof(buf)-1, 0, (sockaddr *)&si_other, &slen);

    printf("recv: %s\n", buf);
}

It is compiled in debug mode, the asserts aren't being erased during compilation, and my program just blocks on recvfrom.

它在调试模式下编译,在编译期间不会擦除断言,我的程序只是在recvfrom.

Is there any other hoop I have to jump through to receive an untargeted UDP broadcast?

是否有任何其他箍我必须跳过以接收无目标的 UDP 广播?

Edit: just a bit more info, I have the two computers connected on a dedicated switch, no outside interference. I also have a second network card on my client computer that connects to the company network, which also works.

编辑:更多信息,我将两台计算机连接在专用交换机上,没有外部干扰。我的客户端计算机上还有一个连接到公司网络的网卡,它也可以工作。

I can ping both the outside (Internet working) and my server machine (plus I can see the actual packets in Wireshark), but you never know what might cause this problem.

我可以 ping 外部(Internet 工作)和我的服务器机器(另外我可以在 Wireshark 中看到实际的数据包),但您永远不知道是什么原因导致了这个问题。

采纳答案by Blindy

As it turns out my code is perfectly fine, as I thought it would be. There was a problem with the network setup itself.

事实证明,我的代码非常好,正如我所想的那样。网络设置本身存在问题。

For posterity, I had set up two static IP'd computers on their own hub, instead of using the built in DHCP server on the server machine to allocate the IP address for the other computer. Pretty localized for my problem but you never know..

为了后代,我在自己的集线器上设置了两台静态 IP 计算机,而不是使用服务器计算机上的内置 DHCP 服务器为另一台计算机分配 IP 地址。我的问题非常本地化,但你永远不知道..

回答by Fl0

To send and receive broadcast

发送和接收广播

  1. Be sure that netmask is correct. in windows mask for broadcast packets does not matters, but not in linux.

  2. bind socket to INADDR_ANY

  3. setsockopt to BROADCAST

  4. call sendto with sendaddr.sin_addr.s_addr = inet_addr("your_interface_broadcast_address"). or - call sento several times for each interface with its broadcast ip address.

  5. call recvfrom. any time before calling recvfrom, set up length parameter.

  1. 确保网络掩码正确。在 windows 中广播数据包的掩码无关紧要,但在 linux 中则不然。

  2. 将套接字绑定到 INADDR_ANY

  3. setockopt 到 BROADCAST

  4. 使用 sendaddr.sin_addr.s_addr = inet_addr("your_interface_broadcast_address") 调用 sendto。或 - 使用其广播 IP 地址为每个接口多次调用 sendo。

  5. 调用 recvfrom。在调用 recvfrom 之前的任何时候,设置长度参数。