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:
- Calling and not calling BindServiceNameAsync() on advertiser.
- Using
MulticastOnly
on advertiser, listener or both - Waiting for a few seconds after creating one object before the other.
- Using
255.255.255.255
as host.