具有多个 NIC 的计算机上的 UDPClient 多播接收失败
我有一台带有多个 NIC 的计算机 - 并且 UDPClient 的发送方法不断失败。代码如下:
private static void receiveData()
{
recvSock = new UdpClient(PORT);
//recvSock.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, mainInterface);
recvSock.JoinMulticastGroup(IPAddress.Parse(IP), 50);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
byte[] data = recvSock.Receive(ref iep);
// Do not include messages from us
if (myIPs.Contains(iep.Address))
continue;
string stringData = Encoding.ASCII.GetString(data, 0, data.Length);
Console.WriteLine("received: " + stringData);
}
}
PORT = 5000 和 IP = 224.5.6.7 所以应该没问题。主要问题是我无法通过recvSock.Receive()行。我看到数据包通过wireshark传入 - 但代码不会处理它们......
有什么想法?提前致谢!
Dan
编辑:我可以确认多网卡导致了问题 --- 代码在单网卡上运行良好。取消注释 SetSocketOption 行应该允许它与多个 NIC 一起工作,但它仍然失败......想法?
I've got a computer with multiple NICs - and UDPClient's send method continually fails. Here's the code:
private static void receiveData()
{
recvSock = new UdpClient(PORT);
//recvSock.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, mainInterface);
recvSock.JoinMulticastGroup(IPAddress.Parse(IP), 50);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
byte[] data = recvSock.Receive(ref iep);
// Do not include messages from us
if (myIPs.Contains(iep.Address))
continue;
string stringData = Encoding.ASCII.GetString(data, 0, data.Length);
Console.WriteLine("received: " + stringData);
}
}
PORT = 5000 and IP = 224.5.6.7 so that should be OK. The main problem is that I just can't get past the recvSock.Receive() line. I see the packets coming in over wireshark - but the code just won't process them...
Thoughts? Thanks in advance!
Dan
EDIT: I can confirm that the multi NICs is causing the problem --- the code works fine with a single NIC. Uncommenting the SetSocketOption line should allow it to work with multiple NICs, but it still fails.... thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,找到了这篇文章,然后在以下位置找到了解决方案:
UDP:从所有网络接口读取数据
基本上 Bind() 到 0.0 .0.0 不起作用,您必须在每个本地 IP 地址上进行 Bind() 和 JoinMulticastGroup() 。一定要喜欢微软这个。
I had the same issue found this post, then found the solution at:
UDP: Read data from all network interfaces
Basically Bind() to 0.0.0.0 doesn't work and you have to Bind() and JoinMulticastGroup() on every local ip address. Gotta love Microsoft for this one.
接口部分是以下代码中的重要部分:
接口是接收网卡的(单播)IP 地址。
The interface part is the important part in the following code:
With interface being the (unicast) IP address of the receive network card.