I managed to create a simple application that sends and receives data from a multicast group. If I open 2 instances of the application (2 different .sln files with the same code), I can send and receive data. The problem is that after 5 seconds, if I send a message with Client001, only the message Client001 will receive the message. But, if I send a message with Client002 (the second instance of the application) within 5 seconds, then both of them will receive the message. I had an example with UdpClient that worked fine, but it is no longer available for UWP. So in conclusion, how can I achieve, no matter when (not within 5 seconds) some of the clients send a message, all the other clients receive it?
This is the code for MainPage.xaml.cs
namespace Client001
{
public sealed partial class MainPage : Page
{
private DatagramSocket listenerSocket = null;
public string remoteAddress = "224.3.0.5";
HostName remoteHostname;
public string serviceName = "22113";
IOutputStream outputStream;
DataWriter writer;
public MainPage()
{
this.InitializeComponent();
SetupMulticastScenarioUI();
remoteHostname = new HostName(RemoteAddress.Text);
}
private void CloseListenerSocket()
{
if (listenerSocket != null)
{
listenerSocket.Dispose();
listenerSocket = null;
}
}
private void SetupMulticastScenarioUI()
{
RemoteAddress.Text = remoteAddress;
ServiceName.Text = serviceName;
StartListener.Content = "Start listener and join multicast group";
SendMessageButton.IsEnabled = false;
CloseListenerButton.IsEnabled = false;
SendOutput.Text = "";
}
private async void StartListener_Click(object sender, RoutedEventArgs e)
{
listenerSocket = new DatagramSocket();
listenerSocket.Control.MulticastOnly = true;
await listenerSocket.BindServiceNameAsync(ServiceName.Text);
listenerSocket.JoinMulticastGroup(remoteHostname);
listenerSocket.MessageReceived += MessageReceived;
SendOutput.Text = "Listening on port " + listenerSocket.Information.LocalPort + " and joined to multicast group";
SendMessageButton.IsEnabled = true;
CloseListenerButton.IsEnabled = true;
outputStream = await listenerSocket.GetOutputStreamAsync(remoteHostname, ServiceName.Text);
writer = new DataWriter(outputStream);
writer.WriteString("Handshake1");
await writer.StoreAsync();
}
private async void SendMessage_Click(object sender, RoutedEventArgs e)
{
writer.WriteString(Message.Text);
await writer.StoreAsync();
}
private void CloseListener_Click(object sender, RoutedEventArgs e)
{
CloseListenerSocket();
SendMessageButton.IsEnabled = false;
CloseListenerButton.IsEnabled = false;
SendOutput.Text = "";
SendOutput.Text = "Listener closed";
}
async void MessageReceived(DatagramSocket socket, DatagramSocketMessageReceivedEventArgs eventArguments)
{
uint stringLength = eventArguments.GetDataReader().UnconsumedBufferLength;
string receivedMessage = eventArguments.GetDataReader().ReadString(stringLength);
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
SendOutput.Text = "Received data from remote peer (Remote Address: " +
eventArguments.RemoteAddress.CanonicalName + ", Remote Port: " +
eventArguments.RemotePort + "): \"" + receivedMessage + "\"";
});
}
}
}
MainPage.xaml
<Page
x:Class="Client001.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Client001"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel>
<TextBlock>Remote Address:</TextBlock>
<TextBox x:Name="RemoteAddress" />
<TextBlock>Service Name:</TextBlock>
<TextBox x:Name="ServiceName" />
<Button x:Name="StartListener" Click="StartListener_Click" Margin="0,10,0,0"/>
<Button x:Name="SendMessageButton" Content="Send 'hello'" Click="SendMessage_Click" Margin="0,10,0,0"/>
<Button x:Name="CloseListenerButton" Content="Close Listener" Click="CloseListener_Click" Margin="0,10,0,0"/>
<TextBlock x:Name="SendOutput" TextWrapping="Wrap" Margin="0,10,0,0"/>
<TextBox x:Name="Message"></TextBox>
</StackPanel>
</Page>
: , , , TTL (Time To Live), , .