Issues with async receiving UDP Unicast packets in

2020-02-12 04:59发布

问题:

I'm trying to get my Windows Phone 7 Mango app to listen to incoming UDP packets but having a heck of a time. I have the new Beta 2 refresh of the Windows Phone 7.1 SDK and dev tools installed. Any deviation I try from this MSDN sample results in a SocketException 10022, "An invalid argument was supplied".

My code is pasted below. I have been trying to adapt the code I found on this stackoverflow post but to no avail. This line generates the exception when its reached:

synchronous = m_udpSock.ReceiveFromAsync(udpRecvArg);

I'm hoping someone here can help identify what's going wrong. I'm calling "StartUnicastListen()" when the user presses a button. m_udpSock is previously defined as a class variable and set to null. Per the "Remarks" section of the ReceiveFromAsync() MSDN page, I've set all of the required properties and events.

private void StartUnicastListen()
{
    m_udpSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    SocketAsyncEventArgs udpRecvArg = new SocketAsyncEventArgs(); 
    udpRecvLoopStart(udpRecvArg);
}

private void udpRecvLoopStart(SocketAsyncEventArgs udpRecvArg) 
{ 
    byte[] udpRecvBuffer = new byte[2048];
    udpRecvArg.SetBuffer(udpRecvBuffer, 0, udpRecvBuffer.Length); 
    udpRecvArg.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 11100); 
    udpRecvArg.Completed += new EventHandler<SocketAsyncEventArgs>(udpRecvArg_Completed); 
    udpRecv(udpRecvArg); 
}    

private void udpRecv(SocketAsyncEventArgs udpRecvArg) 
{ 
     bool synchronous = false; 
     try {
         synchronous = m_udpSock.ReceiveFromAsync(udpRecvArg);
     } catch (SocketException e) {
         Log("recvUdp()\n" + e.SocketErrorCode + "\n" + e.ToString(), false); 
         return; 
     } if (synchronous)
         udpRecvArg_Completed(this, udpRecvArg); 
}

void udpRecvArg_Completed(object sender, SocketAsyncEventArgs udpRecvArg) { 

     EndPoint udpEp = udpRecvArg.RemoteEndPoint; 
     string msg = Encoding.UTF8.GetString(udpRecvArg.Buffer, udpRecvArg.Offset, udpRecvArg.BytesTransferred); 
     Log(udpEp + " " + msg,false); 
     udpRecv(udpRecvArg); 
}

There's such limited documentation the proper usage of ReceiveFromAsync()--which seems to be the only option for this on WP7--and on System.Net.Sockets in Windows Phone 7 in general right now.

Thanks in advance for any help you can provide.

回答1:

I had this same problem, but here is the solution that I came up with. As wilbur4321 said, you have to send something to the socket first. Also, not only do you need to just call SendToAsync and forget about it, you must wait for it (max of 1 second seems to work for me).

I do not know why you are required to do this, but sending something (I just send 1 byte: 0xFF) seems to do the trick.



回答2:

Have you seen the sample at http://msdn.microsoft.com/en-us/library/hh202864(v=VS.92).aspx#Y4537?

Given that TCP sockets are client-only in Mango, I wonder is UDP sockets might only work after something has been sent? I would suggest trying that. If that doesn't work, could you post your entire project on dropbox or the like, and I'll look at it.

Thanks, --randy



回答3:

I found that a strange thing in that "Win7 UDP server" sample that is the server-side UDP socket doesn't "Bind"

in my experiment, call "ReceiveFromAsync" on a socket which doesn't bind, "invalid argument" exception is throw. After binding before "ReceiveFromAsync", at least "ReceiveFromAsync" won't throw "invalid argument" exception.

My codes is similar to yours, but there is still some problems. it seems that, I call "ReceiveFromAsync" once, but the OS fire the event callbacks twice, which cause very serious race condition.

There is so few sample, documents about "UDP + SocketAsyncEventArgs". Maybe I have to revert back to using "BeginReceiveFrom".



回答4:

You need to call ReceiveFormAsync() from the Completed event of SendTAsync()..Otherwise, you will get this exception..