Problems with Asynchronously Receiving Unicast UDP Packets in Windows Phone 7

I am trying to run the Mango application for Windows Phone 7 to listen for incoming UDP packets, but with a lot of time. I have a new Beta 2 update for the Windows Phone 7.1 SDK and developer tools. Any deviation from this MSDN pattern results in SocketException 10022, "invalid argument specified".

My code will be inserted below. I am trying to adapt the code that I found on https://stackoverflow.com/a/3608065/ ... but to no avail. This line throws an exception when it is reached:

synchronous = m_udpSock.ReceiveFromAsync(udpRecvArg); 

I hope someone here helps determine what is going wrong. I call "StartUnicastListen ()" when the user clicks a button. m_udpSock was previously defined as a class variable and is null. In the "Remarks" section of the MSDN ReceiveFromAsync () page, I set all the necessary 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 on the proper use 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.

+8
c # asynchronous udp windows-phone-7 sockets
source share
4 answers

I had the same problem, but here is the solution I came across. As wilbur4321 said, you must first send something to the socket. In addition, you need to not only just call SendToAsync and forget about it, you should wait for it (maximum 1 second seems to work for me).

I don't know why you should do this, but sending something (I will just send 1 byte: 0xFF) seems to do the trick.

+3
source share

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

Given that TCP sockets are intended only for clients in Mango, it is interesting that UDP sockets can work only after something has been sent? I would try this. If this does not work, you can publish the entire project on Dropbox or the like, and I will look at it.

Thank you --randy

+1
source share

I found that the strange thing in this example, “Win7 UDP server” , which is a server-side UDP socket, does not bind to

in my experiment, call "ReceiveFromAsync" on the socket that is not communicating, the exception "invalid argument" is throw. After binding to “ReceiveFromAsync”, at least “ReceiveFromAsync” will not throw an “invalid argument” exception.

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

There are so few examples of documents about "UDP + SocketAsyncEventArgs". Maybe I need to get back to using "BeginReceiveFrom".

+1
source share

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

0
source share

All Articles