仅从特定适配器(例如192.168.101.1)发送UDP广播(255.255.255.255); 在 Windows 上
适用于 Windows XP 或更高版本的解决方案。 最好是 C# 或 C++。
我们不想使用子网定向广播(例如 192.168.101.255)进行广播,因为我们尝试联系的设备没有对此做出响应。 相反,我们希望能够仅从特定的 NIC/IP 地址发送目标为 255.255.255.255 的 UDP 数据报,这样广播就不会在其他 NIC 上发送。
这意味着我们必须绕过 IP 堆栈,这就是问题所在。 我们如何绕过 Windows 上的 IP 堆栈,仅从特定的 NIC/MAC 地址发送符合 UDP/IP 的数据报?
Solution for Windows XP or higher. Preferably in C# or else in C++.
We do not want to broadcast using a subnet directed broadcast (e.g. 192.168.101.255) since the device we are trying to contact is not responding to this. Instead, we want to be able to send UDP datagram with a destination of 255.255.255.255 from a specific NIC/IPAddress only, such that the broadcast is NOT send out on other NICs.
This means we have to circumvent the IP stack, which is, thus, the question. How can we circumvent the IP stack on windows to send a UDP/IP compliant datagram from a specific NIC/MAC address only?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需将套接字绑定()到所需的接口而不是使用 INADDR_ANY ?
Just bind() the socket to the desired interface instead of using INADDR_ANY ?
为了使用 WinPcap 在 C# 中发送原始 pcaket,您可以使用 Pcap.Net。
它是用 C++/CLI 和 C# 编写的 WinPcap 包装器,包括数据包解释和创建框架。
In order to use WinPcap to send a raw pcaket in C#, you can use Pcap.Net.
It's a wrapper for WinPcap written in C++/CLI and C# and includes a packet interpretation and creation framework.
我没有尝试过这个,但我知道 WinPCap 允许你做一个原始的 发送。 由于它在相当低的级别上工作,因此它可能允许您在堆栈上发送足够低的级别以绕过完整的广播。 有各种 C# 包装器,当然您可以使用可用的普通 C/C++ 代码。 我认为技巧可能是绑定到您想要发送的正确适配器,它可能会起作用。
I've not tried this, but I know that WinPCap allows you to do a raw send. Since it works at a pretty low level, it might allow you to send low enough on the stack to bypass the full broadcast. There are various C# wrappers out there, and of course you can use the normal C/C++ code available out there. I think the trick might be to bind to the right adapter you want to send out of and it might just work.
我刚刚在类似问题,基于克里斯·贝克的回应。 这在 C# 中当然是可行的,可能需要更少的行数。
我不知道为什么OP认为它不起作用,除了克里斯可能没有“点所有的i”。 我在那篇文章中展示的代码是从一个工作示例程序复制而来的。
I just posted an answer at a similar question, based on Chris Becke's response. It is certainly do-able in C#, probably in many fewer lines.
I'm not sure why OP thought it didn't work, except perhaps that Chris did not "dot all the i's". The code I showed in that post was copied from a working sample program.
广播地址255.255.255.255太笼统. 您必须分别为每个网络接口制作不同的广播地址。
The broadcast address 255.255.255.255 is too general. You have to craft a different broadcast address for each network interface separately.