UDP multicast with multiple NICs only works with an active interface

I looked through everything and can not find a solution to this problem. I tried every combination that I could see without 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 network adapter is active, but when they are active, it stops working. I looked with Wireshark and with one network adapter I could see how packets come and go.

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

The code is as follows. I used only one socket, but tried something else.

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); } 
+4
source share
3 answers

Mike G is right. One of the constructors of the UDPClient class takes IPEndPoint as an argument. If IPEndPoint is set to the IP address of the local interface, then this is the interface that UDPClient and the base socket will use so, yes, you can have two UDP clients mapped to the same port on the machine if they are on separate local IP Interfaces (e.g. multi-axis or multi-network adapters).

+1
source

I know that this branch is old, but with the same problem, I thought I would participate anyway.

On my sender machine, I have 6 network adapters. But only 1 should 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 that I use to make sure I get the multicast is the right interface.

  • Open cmd as administrator (right click run as administrator)
  • Delete default multicast routes. > route delete 224.0.0.0 mask 240.0.0.0
  • Add the route to the desired network adapter. > route add 224.0.0.0 mask 240.0.0.0 IP_of_NIC
+1
source

I had the same problem in a Windows failover cluster ... Several nics ....

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

This is not true.

You need to specify the IP address of the interface that you want to use to create an IPEndpoint. THEN use this endpoint when creating a socket instead of IPAddress.any

This solved the problem for me.

Hope this helps, even if he is late.

0
source

All Articles