- Google+ Tools
Make Google+ profile picture
Make Google plus banners for profile
Create and share your Google Plus profile banners.
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).
Listening for and receiving UDP packets on a particular local IP and port
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); string ip = "192.168.0.1"; //ip = "0.0.0.0"; //any int port = 26010; socket.Bind(new IPEndPoint(IPAddress.Parse(ip), port)); //socket.Listen(8); -> Listen doesn't work for UDP for some reason var buffer = new byte; int numBytesReceived = socket.Receive(buffer);