1

What's wrong with the following simple arrangement. All I'm doing is to create a UDP advertiser that multicasts a message, and a listener that joins the multicast group to receive this message, both running on the same machine.

string Port = "54153";
HostName Host = new HostName("224.3.0.5"); //a multicast range address

//listener
var L = new DatagramSocket();
L.MessageReceived += (sender2, args) => { /*something*/ };
await L.BindServiceNameAsync(Port);
L.JoinMulticastGroup(Host);

//advertiser
var AdvertiserSocket = new DatagramSocket();
AdvertiserSocket.Control.MulticastOnly = true;

Stream outStream = (await AdvertiserSocket.GetOutputStreamAsync(Host, Port)).AsStreamForWrite();
using (var writer = new StreamWriter(outStream))
{
  await writer.WriteLineAsync("MESSAGE");
  await writer.FlushAsync();
}

The listener doesn't receive anything at all (MessageReceived never invoked). I have tried the following variations without success:

  1. Calling and not calling BindServiceNameAsync() on advertiser.
  2. Using MulticastOnly on advertiser, listener or both
  3. Waiting for a few seconds after creating one object before the other.
  4. Using 255.255.255.255 as host.
dotNET
  • 33,414
  • 24
  • 162
  • 251
  • Please check if [this similar case](http://stackoverflow.com/questions/38679646/uwp-datagramsocket-multicast/38739116#38739116) helps. – Elvis Xia - MSFT Sep 28 '16 at 10:27
  • @ElvisXia-MSFT: Thanks Elvis. The link you provided uses one of the samples (scenario5 in UWP API samples). This is exactly what I started with. The problem with this sample is that it uses a single `DatagramSocket` and advertises and listens on the same socket. In my case, I have two `DatagramSocket` objects running on the same machine; one of them is the advertiser that is multicasting and the other one is the listener which is listening for multicast data. In this particular scenario the listener doesn't receive anything. – dotNET Sep 28 '16 at 10:39
  • @ElvisXia-MSFT: Just copy and paste the above code in a blank UWP application and run it. You'll see the behavior that I'm talking about. – dotNET Sep 28 '16 at 10:41
  • @ElvisXia-MSFT: I dug it further and used [TCPView](https://technet.microsoft.com/en-us/sysinternals/tcpview.aspx) to see which of the two sockets is malfunctioning. It seems like the advertiser is multicasting data correctly (TCPView shows sent packets), but the receiving port is not getting anything. – dotNET Sep 28 '16 at 10:42

1 Answers1

3

It seems like the advertiser is multicasting data correctly (TCPView shows sent packets), but the receiving port is not getting anything.

Thank you for sharing this problem. I made a demo and did some tests. I found out the listener socket won't receive any message until it has sent one message in the group.

So, currently the workaround is to send an empty message immediately after register for listening:

private async void btnListen_Click(object sender, RoutedEventArgs e)
{
        socket = new DatagramSocket();
        socket.MessageReceived += Socket_MessageReceived;
        socket.Control.MulticastOnly = true;
        await socket.BindServiceNameAsync(serverPort);
        socket.JoinMulticastGroup(serverHost);
        SendWithExistingSocket(socket, "");//send an empty message immediately
}

private async void SendWithExistingSocket(DatagramSocket socket, String text)
{
    if (socket != null)
    {
        Stream stream = (await socket.GetOutputStreamAsync(serverHost, serverPort)).AsStreamForWrite();
        using (var writer = new StreamWriter(stream))
        {
            writer.WriteLine(text);
            await writer.FlushAsync();
        }
    }
}

As for the root cause of this problem, I'll consult with related team and let you know once I got response.

Elvis Xia - MSFT
  • 10,801
  • 1
  • 13
  • 24