I wanted to experiment with UDP multicasting on WP8.1 and found this library on nuget.
I have created 2 apps, one on WP8.1 and one on desktop, I will provide only C# code as WPF is irrelevant here.
WP8.1:
public sealed partial class MainPage : Page
{
int port = 11811;
string ip = "239.123.123.123";
UdpSocketMulticastClient udp;
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
}
async protected override void OnNavigatedTo(NavigationEventArgs e)
{
udp = new UdpSocketMulticastClient();
udp.MessageReceived += (sender, args) =>
{
var data = Encoding.UTF8.GetString(args.ByteData, 0, args.ByteData.Length);
Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
lblChat.Text += "\n" + data;
});
};
await udp.JoinMulticastGroupAsync(ip, port);
}
async private void btnSend_Click(object sender, RoutedEventArgs e)
{
var msgBytes = Encoding.UTF8.GetBytes(txtEntry.Text);
await udp.SendMulticastAsync(msgBytes);
}
}
Desktop (almost identical):
public partial class MainWindow : Window
{
int port = 11811;
string ip = "239.123.123.123";
UdpSocketMulticastClient udp;
public MainWindow()
{
InitializeComponent();
udp = new UdpSocketMulticastClient();
udp.MessageReceived += (sender, args) =>
{
var data = Encoding.UTF8.GetString(args.ByteData, 0, args.ByteData.Length);
lblChat.Text += "\n" + data;
};
udp.JoinMulticastGroupAsync(ip, port);
}
async private void btnSend_Click(object sender, RoutedEventArgs e)
{
var msgBytes = Encoding.UTF8.GetBytes(txtEntry.Text);
await udp.SendMulticastAsync(msgBytes);
}
}
Now what is happening (debug on an actual phone):
- messages sent from desktop app don't make it to phone app and vice versa
- messages sent from desktop app are visible in the output on desktop app (as expected)
- messages sent from phone app appear in phone app output 3 times (yes - the MessageReceived event fires 3 times)
And on a WP8.1 emulator (running on the same PC as the desktop app):
- messages are being sent between apps
- messages sent from phone app still appear twice on the phone app output
I was expecting this to be very simple task and as you see the code is minimal. Am I doing something wrong? Or is the library faulty (kind of doubt that)? If anyone used this library before, I would appreciate some help, thanks :)