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?
来源:https://stackoverflow.com/questions/15279519/cant-receive-udp-windows-rt