具有多个 NIC 的计算机上的 UDPClient 多播接收失败

发布于 2024-09-08 10:17:42 字数 1012 浏览 6 评论 0原文

我有一台带有多个 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 技术交流群。

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

发布评论

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

评论(2

一页 2024-09-15 10:17:42

我遇到了同样的问题,找到了这篇文章,然后在以下位置找到了解决方案:
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.

遮云壑 2024-09-15 10:17:42

接口部分是以下代码中的重要部分:

unsigned long interface;
ip_mreq mreq;

_parseHostname( _description->getInterface(), interface );
mreq.imr_multiaddr.s_addr = _writeAddress.sin_addr.s_addr;
mreq.imr_interface.s_addr = interface;

setsockopt( _readFD, IPPROTO_IP, IP_ADD_MEMBERSHIP,
                (char*)&mreq, sizeof( mreq ));

接口是接收网卡的(单播)IP 地址。

The interface part is the important part in the following code:

unsigned long interface;
ip_mreq mreq;

_parseHostname( _description->getInterface(), interface );
mreq.imr_multiaddr.s_addr = _writeAddress.sin_addr.s_addr;
mreq.imr_interface.s_addr = interface;

setsockopt( _readFD, IPPROTO_IP, IP_ADD_MEMBERSHIP,
                (char*)&mreq, sizeof( mreq ));

With interface being the (unicast) IP address of the receive network card.

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