public AsyncMulticastClientWithMultipleUdpClients(IPAddress multicastAddress, int port) { MulticastAddress = multicastAddress; Port = port; var localIpAddresses = NetworkHelpers.GetOtherIpAddresses(); foreach (var localIpAddress in localIpAddresses) { try { UdpClient client = null; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { client = new UdpClient(port); } else { client = new UdpClient { ExclusiveAddressUse = false }; client.Client.Bind(new IPEndPoint(IPAddress.Any, Port)); } Console.WriteLine($"Trying to join {multicastAddress} on {localIpAddress}"); client.JoinMulticastGroup(multicastAddress, localIpAddress); var state = new State { Client = client, LocalIpAddress = localIpAddress }; Clients.Add(client); client.BeginReceive(ReceiveCallback, state); Console.WriteLine($"{localIpAddress}:{port} Worked"); } catch (Exception e) { Console.WriteLine($"{localIpAddress}:{port} Did not work: {e}"); } } }
public AsyncMulticastClientWithTap(IPAddress multicastAddress, int port) { MulticastAddress = multicastAddress; Port = port; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { Client = new UdpClient(port); } else { Client = new UdpClient { ExclusiveAddressUse = false }; Client.Client.Bind(new IPEndPoint(IPAddress.Any, Port)); } CancellationSource = new CancellationTokenSource(); Console.WriteLine( $"==> ExclusiveAddressUse {Client.ExclusiveAddressUse} MulticastLoopback {Client.MulticastLoopback}"); var localIpAddresses = NetworkHelpers.GetIpAddresses(); foreach (var localIpAddress in localIpAddresses) { try { Client.JoinMulticastGroup(multicastAddress, localIpAddress); Console.WriteLine($"{localIpAddress} Worked"); } catch (Exception e) { Console.WriteLine($"{localIpAddress} Did not work"); } } Task.Factory.StartNew(ReceiveNextMessage, CancellationSource.Token); }