Windows 7 中的 UDP 广播 - 有效吗?

发布于 2024-10-10 19:31:04 字数 1316 浏览 2 评论 0原文

我正在尝试在 Windows 7 下编写一些代码以在我的本地网络上进行广播,但无法使以下代码正常工作。我来自 Linux 背景,所以对风格表示歉意 - 完整的代码编译等并工作,如果我使用以下地址:

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

那么工作正常,我只是真的很想使用首选的 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);
}

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. I come from a Linux background so apologies for style - the full code compiles etc and works and if I use an address of:

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

大姐,你呐 2024-10-17 19:31:04

Windows 7 中的 UDP 广播存在一个巨大错误,导致 255.255.255.255 上的广播无法在大多数 Windows 7 安装上运行:https://serverfault.com/questions/72112/how-to-alter-the-global -broadcast-address-255-255-255-255-behavior-on-windows

基本上,它只会在单个网络接口上发送广播,该网络接口可以是任何东西,甚至是虚拟机网络接口或蓝牙接口之类的东西,最终可能不会广播到任何设备。

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

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.

九歌凝 2024-10-17 19:31:04

除非我的数学算出来了,inet_addr("192.168.10.0") | ~(inet_addr("255.255.240.0"))inet_addr("192.168.15.255") 相同,后者是该子网的广播地址。

在我看来,最有可能的可能性不是发送代码错误,而是接收代码错误。您将接收套接字绑定到什么地址?它位于哪个子网?

Unless my bit maths is out, 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.

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文