5

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?

Dan
  • 51
  • 1
  • 3
  • To be clear, this "client" application [machine] has multiple NIC's or the the server your connecting to has multiple NIC's?? – Robert Seder Jul 06 '10 at 00:46
  • Since I am having the same issue, I am willing to confirm: "the client application [machine] has multiple NICs". Typically the client is concerned with servers being on any of the multiple NIC networks. A server that has multiple NICs is only important if the client can see the _same_ server via multiple client side NICs. – Jesse Chisholm Jul 19 '12 at 22:18
  • @Dan Did you find a workaround? I'm having the same issue – J4N Mar 07 '13 at 15:21

2 Answers2

1

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.

Community
  • 1
  • 1
karl
  • 307
  • 1
  • 11
0

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.

eile
  • 1,153
  • 6
  • 18
  • 1
    This answer _may_ be useful. But since it uses terminology from an radically different socket package from the original question, ..., who can tell? – Jesse Chisholm Jul 19 '12 at 22:22
  • 1
    The only relevance this answer has to the given question is that they both contain the word "IP." – BTownTKD Jan 29 '13 at 15:45