URL: http://msdn.microsoft.com/en-us/magazine/cc163648.aspx
For some reason, the only .NET way to listen for UDP packets seems to be the Bind and Receive(Async) methods of the Socket class. The Listen method won't work with UDP in my tests.
As for releasing the Socket correctly, this forum post explains that you must call Socket.Shutdown instead of Socket.Close (it also says to call Socket.Disconnect(true), but I get exceptions when I do that). If you want to reuse the local endpoint (IP and port), then do Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
While debugging Socket issues, it's helpful to have a command-line open and use the netstat tool (e.g. "netstat -a -p UDP" to show all UDP sockets on all local interfaces).
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); string ip = "192.168.0.1"; //ip = "0.0.0.0"; //any int port = 26010; //socket.Listen(8); -> Listen doesn't work for UDP for some reason int numBytesReceived = socket.Receive(buffer);
You need to login to post a comment.
