开发者

UDPClient Multicast receive fails on computer with multiple NICs

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 recvSoc开发者_StackOverflowk.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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜