How to receive UDP packets from any ip and any port?

raisyn picture raisyn · Jul 21, 2010 · Viewed 15.6k times · Source

I wanted to use C#'s UdpClient to listen to any incomming UDP packets. I want to receive packets from any IP and any port.

I tried the following:

UdpClient udpClient = new UdpClient(0);
IPEndPoint ep = new IPEndPoint(IPAddress.Any, 0);
byte[] data = udpClient.Receive(ref ep);

but without success.

Does anyone know whats wrong? Thanks in advance!

Answer

bobobobo picture bobobobo · Jul 21, 2010

RECEIVE on any port? That's insane. You would be flooded with messages from other applications (try TcpView for an idea of how many messages get passed on your system per second!)

You must specify a port! Port is sort of like an identifier -- this packet is meant for THIS program (identified by port #)

Send on any port is sensible, as it asks the system to choose a port send OUT port for you -- which isn't really that important to your application as the sender sometimes