Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JoinMulticastGroupAsync throws exception with ICommsInterface.BroadcastAddress #122

Open
ChristophvanderFecht opened this issue Dec 14, 2016 · 1 comment

Comments

@ChristophvanderFecht
Copy link

Hi,

I am using the following code to send a broadcast to all network interfaces known to my machine.

public async Task BroadcastTest()
{
    var interfaces = await CommsInterface.GetAllInterfacesAsync();
     interfaces = interfaces.Where(x => x.IsUsable == true && x.BroadcastAddress.StartsWith("169.254.") == false)
                .ToList();

    int port = 50002;
    var udpClient = new UdpSocketMulticastClient();
    udpClient.TTL = 5;

    udpClient.MessageReceived += OnMessageReceived;

    string message = "Broadcast hello";
    byte[] messageBytes = Encoding.UTF8.GetBytes(message);

    foreach (var currentInterface in interfaces)
    {
        try
        {
            await udpClient.JoinMulticastGroupAsync(currentInterface.BroadcastAddress, port,  currentInterface);
            await udpClient.SendMulticastAsync(messageBytes);

            await Task.Delay(2000);
        }
        catch (Exception ex)
        {
              throw ex;
         }
    }
}

As you can see I am fetching all interfaces, filter them by removing those which have a std ip address and then send the message on the remaining interfaces.
But when i run the code the line

await udpClient.JoinMulticastGroupAsync(currentInterface.BroadcastAddress, port, currentInterface);

Throws the following exception:

The parameter is incorrect.

'host': This is not a valid multicast address.

Has anybody here also got this error and knows the solution to it? The Broadcast address seams to be correct and I am not manipulating it on the way.
At my first try i just provided the method with the address and the port since this wasn't working i figured might when i provide the concrete interface to use it will work but sadly no changes.

Thx for your help!
bg

@rdavisau
Copy link
Owner

Howdy,

Broadcast and Multicast are similar but different concepts. The reason you get that error is that there are specific 'multicast addresses' that can be used for multicast, and the broadcast address is not one of those. To broadcast, you should just send data to your preferred port at the broadcast addresses using a UdpClient and receive by listening on the same port.

Let me know how you go!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants