在 .NET 中指定 UDP 多播应进入哪个网络接口
在一台同时具有活动无线卡和 LAN 端口并通过交叉电缆连接到运行相同应用程序的另一台计算机的计算机上,我们需要通过 LAN 线路向另一台计算机发送 UDP 多播。使用 C# 套接字,Windows 似乎每次都会尝试通过 WLAN 适配器路由消息。
有没有办法指定在哪个网络接口上发送 UDP 多播?
On a computer with both an active Wireless Card and a LAN-Port with a crossover cable hooked up to another machine running the same application, we need to send a UDP multicast over the LAN wire to the other computer. Using C# Sockets, Windows seems to try to route the message over the WLAN adapter every time.
Is there a way to specify what network interface to send a UDP multicast on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如尼古拉回答的附录:KB318911 的问题是一个肮脏的伎俩,用户必须提供必要的适配器索引。在寻找如何检索此适配器索引时,我想出了这样的秘诀:
完整说明位于 http://windowsasusual.blogspot.ru/2013/01/socket-option-multicast-interface.html
Just as addendum to Nikolai answer: the problem with KB318911 is a dirty trick that user must provide necessary adapter index. While looking how to retrieve this adapter index I figured out such recipe:
Full note at http://windowsasusual.blogspot.ru/2013/01/socket-option-multicast-interface.html
您可能正在寻找
SocketOptionName.MulticastInterface
。 这里是 MSDN 上的一篇文章,可能会对您有所帮助。除此之外,如果您更新本地路由表以具有与多播地址匹配并指向正确接口的精确条目,那么它应该可以正常工作。
You are probably looking for
SocketOptionName.MulticastInterface
. Here's an article on MSDN that might help you.Other then that if you update your local routing table to have an exact entry matching the multicast address and pointing to the right interface it should just work.
根据您正在执行的操作,有一种 Win32 方法可能会有所帮助。它将返回给定 IP 地址的最佳接口。要获得默认的(0.0.0.0),这通常是您想要的多播,这非常简单:
P/Invoke 签名:
然后在其他地方:
Depending on what you're doing, there's a Win32 method that might help. It'll return the best interface for a given IP address. To get the default one (the 0.0.0.0), which is usually what you want for multicast, it's pretty easy:
P/Invoke signature:
Then somewhere else:
如果您使用 UDPClient 类,则此方法会强制 IGMP 消息离开您想要的接口(第二个参数),即使绑定不起作用。
否则,
MulticastOption(IPAddress, IPAddress)
将起作用。第一个参数是多播地址,第二个地址强制使用您指定的本地端点。If you are using UDPClient class, then this method forces the IGMP message out of the interface you desire (second parameter), even when binding doesn't work.
Otherwise,
MulticastOption(IPAddress, IPAddress)
will work. The first parameter is the multicast address, the second address forces a localendpoint you specify to be used.