接收UDP广播 [英] Receiving UDP broadcast

查看:89
本文介绍了接收UDP广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须接收UDP广播(如果有区别,请在Ubuntu中).使用Wireshark,我可以看到正在从服务器计算机发送的数据包,并且可以看到我的客户端计算机正在接收该数据包,但是我的程序是完全可以忽略的.这就是我所拥有的:

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:

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);
}

它是在调试模式下编译的,在编译过程中不会清除断言,而我的程序只是在 recvfrom 上阻塞.

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

我是否还需要跳过其他箍以接收未定向的UDP广播?

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

仅需更多信息,我将两台计算机连接在专用交换机上,没有外界干扰.我的客户端计算机上还有第二张网卡,该网卡连接到公司网络,该网卡也可以工作.

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.

我可以ping通外部设备(可以正常工作的Internet)和我的服务器计算机(再加上我可以在Wireshark中看到实际的数据包),但是您永远不知道是什么原因导致了此问题.

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.

推荐答案

事实证明,正如我认为的那样,我的代码非常完美.网络设置本身存在问题.

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

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

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..

这篇关于接收UDP广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆