4

I'm writing a Windows Store/Metro/Modern/RT app for Windows 8/RT that needs to receive UDP packets on port 49030, but I can't seem to receive any packets. I've followed the tutorial on using DatagramSocket to the letter, and I'm getting nothing. I know my sender program is sending data, as I can see it on wireshark. I also wrote a test C# console app that uses the regular BSD socket API (System.Net.Sockets.Socket) that properly receives the data over UDP.

This is the code that works:

Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
s.Bind(new IPEndPoint(IPAddress.Any, 49030));
byte[] buf = new byte[5000];
while (true)
{
    Console.WriteLine("Received " + s.Receive(buf) + " bytes.");
}

This outputs lines that report the number of bytes that are being sent, like expected.

My code for the RT app:

public async void StartListening()
{
    DatagramSocket s = new DatagramSocket();
    s.MessageReceived += s_MessageReceived;
    await s.BindServiceNameAsync(this._port.ToString());   
}

void s_MessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs e)
{
    System.Diagnostics.Debug.WriteLine("Hark! A packet!");  // Breakpoint here
}

After calling StartListening(), the breakpoint is never hit, and nothing is printed to the output log.

I tried running the DatagramSocket sample offered by MSFT, and it worked no problem (of course). I can see that the socket is getting opened/listened on, because it shows up in resmon.exe. I also have all of the proper capabilities enabled in the manifest for the app. I've tested it on my x86 laptop and on my Surface RT (remote debugging), and they both exhibit the same behavior.

Any ideas as to why it isn't working?

Matthew Kennedy
  • 616
  • 4
  • 19
  • This question indicates that `socket.Bind` does not appear to work and you need to use a different method: http://stackoverflow.com/questions/10290945/correct-use-of-udp-datagramsocket – Sam Axe Mar 07 '13 at 19:06
  • Interesting. Seems like this might work, but I need to be able to listen on all/unassigned (`s.Bind(new IPEndpoint(IPAddress.Any, port))`). Is there a way to do that with ConnectAsync()? – Matthew Kennedy Mar 07 '13 at 19:18
  • Have you modified your appxmanifest to allow networking ? – Nicolas Voron Aug 13 '15 at 11:04

0 Answers0