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.
c # asynchronous udp windows-phone-7 sockets
greenlake
source share