4

I have looked all over and cannot find a solution to this problem. I have tried every combination I could see with no luck.

Basically, I would like to choose an interface, start a UDP client on two machines and Send/Receive messages. Everything works fine when only one NIC is active, but when two are active, it stops working. I have looked with Wireshark and with one NIC can see packets coming in and going out.

Now when I use two NICs, I can only TX from the first enumerated one and cannot receive on either. WireShark does not show any received packets on the port for either of the two NICs when they are both active.

The code is the following. I used to just have one socket but was trying some different things.

public UDPInstance(IPAddress ip, int port, int RXFrequency)
{
    rxFreq = RXFrequency;
    // Listener Init
    TXclient = new UdpClient();
    RXclient = new UdpClient();
    TXclient.ExclusiveAddressUse = false;
    RXclient.ExclusiveAddressUse = false;
    //localEp = new IPEndPoint(ip, port);
    TXlocalEp = new IPEndPoint(ip, port);
    RXlocalEp = new IPEndPoint(IPAddress.Any, port);
    TXclient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
    RXclient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);            

    TXclient.Client.Bind(TXlocalEp);
    RXclient.Client.Bind(RXlocalEp);
    InterfaceIP = ip.ToString();

    multicastaddress = IPAddress.Parse("239.0.0.222");
    TXclient.JoinMulticastGroup(multicastaddress);
    RXclient.JoinMulticastGroup(multicastaddress);
    // Sender Init
    remoteep = new IPEndPoint(multicastaddress, port);
    Listener = null;
    RXData = new List<string>();
    StartListenerThread();
}

public void StartListenerThread()
{
    Listener = new Thread(new ThreadStart(ListenerThread));
    Listener.IsBackground = true;
    Listener.Start();
}

public void StopListenerThread()
{
    Listener.Abort();
}

private void ListenerThread()
{
    while (true)
    {
        Byte[] data = RXclient.Receive(ref remoteep);
        string datastr = Encoding.Unicode.GetString(data);
        if (datastr != "")
        {
            string[] PacketStrings = datastr.Split(new char[] { '~' });
            foreach (string pkt in PacketStrings)
                RXData.Add(pkt);
        }
        Thread.Sleep(rxFreq);
    }
}

public void Transmit(string data)
{
    byte[] buffer;
    buffer = Encoding.Unicode.GetBytes(data);
    TXclient.Send(buffer, buffer.Length, remoteep);
}
Jalal Said
  • 15,906
  • 7
  • 45
  • 68
Eric Carlson
  • 69
  • 1
  • 3
  • Does WireShark showing that the connection is successfully established when using two NICs? – Jalal Said Aug 07 '11 at 23:37
  • I guess I am not exactly sure what you mean. UDP is connectionless isn't it? I can see that packets are successfully transmitted on the wireless interface, but they are not on the wired one when using two NICs. If I run netstat -ap UDP, then I see an active connection on that port when the application is running only for the wireless device (which is enumerated first). – Eric Carlson Aug 08 '11 at 00:52
  • is this a code issue or something at the os level. does other software work fine - can you use netcat to make connections from one to the other, for example? there no way you could have set them both with the same mac address(?!) is there? – andrew cooke Aug 08 '11 at 01:06
  • @Eric: If you see an active connection on that port, then propably this is a problem when using multi Nics, check [this](http://stackoverflow.com/questions/3182303/udpclient-multicast-receive-fails-on-computer-with-multiple-nics) similar quesiton. – Jalal Said Aug 08 '11 at 01:11
  • @Andrew Cooke: The MAC addresses are not the same. I don't really know how to test it with other software, and it seems odd that it would be OS level, but it as far as I can tell there have been no problems with other software working. – Eric Carlson Aug 08 '11 at 01:29
  • @Jalal Aldeen Saa'd: I saw that post, but I don't see what I am doing differently. I am binding a socket to an Address-Port pair for transmit, and any IP, portNum for receive. I get the same result if I bind both sockets to the interface IP and Port, or if I only use one socket for sending and receiving (as would normally be done) – Eric Carlson Aug 08 '11 at 01:31
  • http://stackoverflow.com/questions/5284852/combine-netcat-with-chat-on-bash-for-automatic-udp-responses has an example of a udp chat using unix command line tools. that would work as a check, i think. – andrew cooke Aug 08 '11 at 01:34
  • @Andrew Cooke: I should note that I am developing in Windows. – Eric Carlson Aug 08 '11 at 02:10
  • @EricCarlson : How did you resolve your issue? I'm having the same kind of problem here: http://stackoverflow.com/questions/15265620/udp-read-data-from-all-network-interfaces – J4N Mar 07 '13 at 08:23
  • Wow, so many answers that ignore the 'multiple NICs' part of the question. Can anyone confirm that the lowest ranked answer from Dave, the one that actually addresses the issue and isn't a fudge, is correct? – Derf Skren Oct 13 '15 at 04:47

3 Answers3

1

Mike G is correct. One of the constructors for the UDPClient class takes an IPEndPoint as an argument. If the IPEndPoint is set to the IP address of a local interface, then that is the interface that the UDPClient and underlying socket will use so yes, you can have two UDP clients bound to the same port on a a machine as long as they are on seperate local IP interfaces (i.e. multi-homed or multi-NIC).

dave
  • 191
  • 1
  • 2
1

I know this thread is old, but having the same problem, I thought I would contribute anyway.

On my 'sender' machine, I have 6 NICs. But only 1 needs to be able to send multicast messages, so I used this trick from http://sinclairmediatech.com/using-multicast-on-windows-with-multiple-nics/ :

A little trick I use to make sure I am getting the multicast on the right interface.

  1. Open cmd as administrator (right click run as administrator)
  2. Delete the default multicast routes. > route delete 224.0.0.0 mask 240.0.0.0
  3. Add the route to the NIC you want. > route add 224.0.0.0 mask 240.0.0.0 IP_of_NIC
jhfelectric
  • 572
  • 2
  • 6
  • 24
0

I had the same issue on a windows failover cluster... Multiple nics....

I ended up opening a case with Micorsoft as I thought it was an OS issue.

It wasn't.

You need to specify the IP of the interface you whant to use to create a IPEndpoint. THen use that endpoint when creating the socket instead of IPAddress.any

That solved the problem for me.

Hope it helps even if it is late.

MIke G
  • 1
  • 2
    So, are you saying that for multiple-NIC it is necessary to create multiple-listener sockets? Once for each NIC, bound to that specific NIC IP Address? – Jesse Chisholm Jul 19 '12 at 21:14