简体   繁体   English

Windows 7中的UDP广播-是否有效?

[英]UDP Broadcast in Windows 7 - does it work?

I'm trying to write some code under Windows 7 to broadcast across my local network and can't get the following code to work. 我正在尝试在Windows 7下编写一些代码以在本地网络上广播,但是无法使以下代码正常工作。 I come from a Linux background so apologies for style - the full code compiles etc and works and if I use an address of: 我来自Linux背景,因此对样式表示歉意-完整的代码可以编译并正常工作,并且如果我使用以下地址:

unsigned long broadcastAddr = inet_addr("192.168.10.0") | ~(inet_addr("255.255.240.0"));

Then that works fine, I just would really like to use the preferred INADDR_BROADCAST/255.255.255.255 method. 这样就可以了,我只是真的想使用首选的INADDR_BROADCAST / 255.255.255.255方法。

<snip>
SOCKET sockfd;
int broadcast = 1;

WSADATA wsaData;    // Windows socket

// Initialize Winsock
if (WSAStartup(MAKEWORD(2,2), &wsaData) == SOCKET_ERROR) {
    perror("WinSock Error");
    getc(stdin);
    exit(EXIT_FAILURE);
}
if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
    perror("Socket Error");
    getc(stdin);
        exit(1);
}

if ((setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (char *)&broadcast, sizeof(broadcast))) == SOCKET_ERROR) {
    perror("Setsockopt - SOL_SOCKET");
    getc(stdin);
    exit(1);
}

struct sockaddr_in recvaddr;
recvaddr.sin_family = AF_INET;
recvaddr.sin_port = htons(PORT);
recvaddr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
memset(recvaddr.sin_zero,'\0', sizeof(recvaddr.sin_zero));

int numbytes = 0;
while ((numbytes = sendto(sockfd, greet, strlen(greet) , MSG_DONTROUTE, (struct sockaddr *)&recvaddr, sizeof(struct sockaddr_in))) != -1) {
        printf("Sent a packet %d\n", numbytes);
        Sleep(100);
}

There is a huge bug in Windows 7 for UDP broadcast which makes broadcasting on 255.255.255.255 not work on most windows 7 install: https://serverfault.com/questions/72112/how-to-alter-the-global-broadcast-address-255-255-255-255-behavior-on-windows Windows 7中存在一个用于UDP广播的巨大错误,这使得在255.255.255.255上进行广播无法在大多数 Windows 7安装中正常工作: https : //serverfault.com/questions/72112/how-to-alter-the-global-broadcast-地址-255-255-255-255-Windows行为

Basically it will send the broadcast only on a single network interface, which could be anything, even something like a VM network interface or bluetooth one, which can end up not broadcasting to any device. 基本上,它将仅在单个网络接口上发送广播,该接口可以是任何东西,甚至可以是VM网络接口或蓝牙之类的东西,最终可能无法向任何设备广播。

Unless my bit maths is out, inet_addr("192.168.10.0") | ~(inet_addr("255.255.240.0")) 除非我的数学inet_addr("192.168.10.0") | ~(inet_addr("255.255.240.0"))不完整,否则inet_addr("192.168.10.0") | ~(inet_addr("255.255.240.0")) inet_addr("192.168.10.0") | ~(inet_addr("255.255.240.0")) is the same as inet_addr("192.168.15.255") which is the broadcast address for that subnet. inet_addr("192.168.10.0") | ~(inet_addr("255.255.240.0"))inet_addr("192.168.15.255")相同,后者是该子网的广播地址。

It looks to me like the most likely possibility is not that the sending code is wrong but that the receiving code is wrong. 在我看来,最可能的可能性不是发送代码错误,而是接收代码错误。 What address have you bound the receiving socket to? 您将接收套接字绑定到了哪个地址? What subnet is it on? 它在哪个子网上?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM