gpt4 book ai didi

c# - 仅从特定适配器(例如 192.168.101.1)发送 UDP 广播(255.255.255.255);在 Windows 上

转载 作者:可可西里 更新时间:2023-11-01 09:57:09 25 4
gpt4 key购买 nike

适用于 Windows XP 或更高版本的解决方案。最好使用 C# 或 C++。

我们不想使用子网定向广播(例如 192.168.101.255)进行广播,因为我们尝试联系的设备没有对此做出响应。相反,我们希望能够仅从特定 NIC/IP 地址发送目标为 255.255.255.255 的 UDP 数据报,这样广播就不会在其他 NIC 上发送。

这意味着我们必须绕过 IP 堆栈,这就是问题所在。我们如何绕过 Windows 上的 IP 堆栈,仅从特定的 NIC/MAC 地址发送符合 UDP/IP 的数据报?

最佳答案

只需将套接字 bind() 到所需的接口(interface)而不是使用 INADDR_ANY 吗?

// Make a UDP socket
SOCKET s = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
// Bind it to a particular interface
sockaddr_in name={0};
name.sin_family = AF_INET;
name.sin_addr.s_addr = inet_addr("192.168.101.3"); // whatever the ip address of the NIC is.
name.sin_port = htons(PORT);
bind(s,(sockaddr*)name);

关于c# - 仅从特定适配器(例如 192.168.101.1)发送 UDP 广播(255.255.255.255);在 Windows 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/807651/

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