UDP WinSock-不接收广播数据包 [英] UDP WinSock - Not Receiving Broadcast Packets

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

问题描述

我正在测试一个简单的套接字设置,其中服务器在指定的端口上侦听,并且客户端发送广播的数据包,该服务器应接收该广播的数据包。

I'm testing a simple socket setup in which a server listens on a specified port, and a client sends a broadcast packet which should be received by that server.

此设置在直接发送消息(即不广播)时很好用,但是在广播时,服务器上再也没有收到数据包。

This setup works fine when sending messages directly (i.e. not broadcasting), but when broadcasting the packet is never received on the server.

一些代码(用错误检查已删除,为简单起见):

Some of the code (trimmed down with error checking removed, for simplicity):

// Client (broadcast sender)

// Create and bind the client socket
clientSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

sockaddr_in sockAddr;
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(5678);
sockAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);

bind(clientSocket, (sockaddr*)(&sockAddr), sizeof(sockAddr));

u_long uMode = 1;
ioctlsocket(clientSocket, FIONBIO, &uMode);

char broadcast = 1;
setsockopt(clientSocket, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast));

// ...

// Send the packet
sockaddr_in sockAddress;
sockAddress.sin_family = AF_INET;
sockAddress.sin_addr.S_un.S_addr = htonl(INADDR_BROADCAST);
sockAddress.sin_port = htons(5679);

char const* pPacket = "Test";
size_t uPacketSize = strlen(pPacket) + 1;

sendto(clientSocket, pPacket, (int)uPacketSize, 0, (sockaddr*)&sockAddress, sizeof(sockAddress));

-

// Server (listener)

// Create and bind the server socket
serverSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

sockaddr_in sockAddr;
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(5679);
sockAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);

bind(serverSocket, (sockaddr*)(&sockAddr), sizeof(sockAddr));

u_long uMode = 1;
ioctlsocket(serverSocket, FIONBIO, &uMode);

// ...

char pBuffer[1024];
while (true)
{
    int iRecvSize = recv(serverSocket, pBuffer, 1024, 0);
    if (iRecvSize)
    {
        printf("Received packet\n");
    }
}


推荐答案

(应该是评论,但我的声誉还不够高)

(Should be a comment, but my reputation is not high enough)

我不知道这是否适用于您,但是最新版本的广播存在不直观的行为Windows。如果您有多个物理以太网适配器,则只能在首选接口(其中首选由Windows的路由表确定)上接收广播。

I don't know if this applies to you, but there is a unintuitive behavior with broadcasts on recent versions of Windows. If you have multiple physical Ethernet adapters, broadcasts will only be received on the "preferred" interface (where "preference" is determined by Windows' routing table)

请参阅以下内容有关说明和可能的解决方法:
https://github.com/dechamps/WinIPBroadcast

See the following for an explanation and potential fix: https://github.com/dechamps/WinIPBroadcast

另一个临时解决方案是禁用所有其他网络适配器,以确保在正确的适配器上收到广播(在控制面板/网络和共享中心/更改适配器设置中) )。

Another temporary fix would be to disable all other network adapters to make sure the broadcast is received on the correct one (in Control Panel/Network and Sharing Center/Change adapter settings).

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

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