gpt4 book ai didi

c++ - UDP WinSock - 未接收广播数据包

转载 作者:行者123 更新时间:2023-11-30 16:53:05 25 4
gpt4 key购买 nike

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

当直接发送消息(即不广播)时,此设置工作正常,但广播时服务器永远不会收到数据包。

一些代码(为了简单起见,删除了错误检查,进行了删减):

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

最佳答案

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

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

请参阅以下内容以获取说明和潜在的修复: https://github.com/dechamps/WinIPBroadcast

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

关于c++ - UDP WinSock - 未接收广播数据包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41022253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com