JoinMulticastGroup throws SocketException continuously

32 views Asked by At

I'm trying to join a multicast group with UdpClient via this code when starting my service:

private IPAddress SI_int;
private readonly UdpClient udpClient = new UdpClient(37777);

public Listener()
{
    Initialize();
}

private async void Initialize()
{
    for (int i = 0; i < retries; i++)
    {
        try
        {
            SI_int = CommunicationHandler.GetEthernetAddress();

            udpClient.JoinMulticastGroup(System.Net.IPAddress.Parse("232.192.29.40"), SI_int);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
            Console.WriteLine(ex.Message);
            await Task.Delay(waitTime * 1000);
        }
}
}

Here's my method for getting the ethernet address:

public static IPAddress GetEthernetAddress()
{
    return NetworkInterface
        .GetAllNetworkInterfaces()
        .Where(n => n.OperationalStatus == OperationalStatus.Up)
        .Where(n => n.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
        .Where(n => n.Name.Contains("Ethernet"))
        .SelectMany(n => n.GetIPProperties()?.UnicastAddresses)
        .Where(n => n.Address.AddressFamily == AddressFamily.InterNetwork)
        .Select(g => g?.Address)
        .Where(a => a != null)
        .FirstOrDefault();
}

However I keep getting this error:

An invalid argument was supplied
   at System.Net.Sockets.Socket.setMulticastOption(SocketOptionName optionName, MulticastOption MR)
   at System.Net.Sockets.Socket.SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, Object optionValue)
   at System.Net.Sockets.UdpClient.JoinMulticastGroup(IPAddress multicastAddr, IPAddress localAddress)
   at RemoteSystemManagerService.Listener.<Initialize>d__8.MoveNext() in C:\Subversion\remsysman\branches\Jonathans\RemoteSystemManagerAgent\Listener.cs:line 48

I have tried finding more info online but haven't been able to find much on this exception when joining a group.

When testing on a system it does seem to receive data in the multicast group, so why this error appears is really odd to me.

0

There are 0 answers