I am trying to implement an UDP listener on a specific port but from any machine. I am trying to use the "new" Universal Windows project using Visual Studio 2015.
Using a WPF "old" type of project I could do the following:
public void StartListening()
{
this.client = new System.Net.Sockets.UdpClient(5606);
this.endpoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 5606);
this.client.BeginReceive(new AsyncCallback(receive), this);
}
private static void receive(IAsyncResult result)
{
var self = ((UDPListener)result.AsyncState);
var receivedBytes = self.client.EndReceive(result, ref self.endpoint);
// do something with receivedBytes
self.StartListening();
}
However using Universal Windows it seems to be quite different. There is no System.Net.Sockets.UdpClient
any more. The only thing I can find is connecting to/from a client/server UDP things and stuffs using Windows.Networking.Sockets.DatagramSocket
. With which I came up with the following:
public async void Connect()
{
var listenerSocket = new Windows.Networking.Sockets.DatagramSocket();
listenerSocket.MessageReceived += ListenerSocket_MessageReceived;
await listenerSocket.BindServiceNameAsync("5606");
}
private void ListenerSocket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args)
{
throw new NotImplementedException();
}
But this does not seem to do what I want. I never receive any data from a server that is running in the background. Where the WPF version does receive data.
What am I doing wrong? Is this not even possible any more? Can Universal Windows applications only receive data from other Universal Windows applications? Or am I just looking at the wrong things here?