Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues with async receiving UDP Unicast packets in Windows Phone 7

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.

like image 607
greenlake Avatar asked Jul 01 '11 17:07

greenlake


2 Answers

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.

like image 89
Nick Banks Avatar answered Nov 14 '22 23:11

Nick Banks


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

like image 34
wilbur4321 Avatar answered Nov 15 '22 00:11

wilbur4321