3

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!

raisyn
  • 4,514
  • 9
  • 36
  • 55
  • Pretty sure that you cannot listen to 'every' port like that... don't see why you would want to, either. – Fosco Jul 21 '10 at 15:47
  • 1
    @Fosco: Network sniffer? – MPritchard Jul 21 '10 at 15:47
  • 1
    @MPritch: Then that's not really UDP. You'd be listening on raw ethernet packets. – Steven Sudit Jul 21 '10 at 15:51
  • Fosco and Steven Sudit (below) are correct. You can not not listen to 'every' port by specifying a port of 0. What happens is that the system picks an ephemeral port for you. You are now listening for UDP packets on a port you didn't pick and no application knows to send to. It does make sense to specify port 0 and use an ephemeral port if you are sending UDP datagrams or if you have a separate channel (of any kind) for sending the ephemeral port number to the sending applications. – Peter Schaeffer Nov 11 '13 at 04:22
  • 1
    re: Network Sniffer? **Then that's not really UDP.** -- then call it **UDP Sniffer**. ;-D – Jesse Chisholm Sep 25 '14 at 20:56

3 Answers3

6

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

bobobobo
  • 64,917
  • 62
  • 258
  • 363
  • 3
    Incase or not, there are legitimate reasons to want this. And it is do-able with RAW+UDP socket in promiscuous mode (and running at Administrator level). – Jesse Chisholm Sep 25 '14 at 20:57
  • 1
    How is it "insane" to receive on any port, but not insane to *send* on any port? If the logic is that that the port identifies which program the packets are meant for, that would apply to both sending and receiving, right? – Mud Aug 10 '17 at 16:16
  • @Mud Not really. Lets use a simple example of receiving communication from two database servers. You want to receive data from both servers and the ports identify which is which. You don't care which ports on the respective servers that they were sent from. That's not your concern. Just that the right ports receive it. – The Betpet Aug 03 '23 at 14:55
  • My point is that it's not "insane" for the machine to receive on every port, because in fact *it already does*. Ports simply control routing of those packets from the network layer to the application layer. – Mud Aug 15 '23 at 19:13
5

Your best idea would be to identify specific ports you would like to listen to, and start listening on those. Depending on what is done with received datagrams, it might be best/simplest to create a new Thread for each port you are listening on, and process it there, or enqueue it on a synchonrised (with lock) queue or list, for processing on a central thread.

You should limit the ports though; it would not be possible to listen to them all.

That said you could use something like Wireshark or the Winpcap SDK/API to 'sniff' UDP packets right from the network adapter. I have had it working within a .NET application before without too much difficulty.

Hope that helps.

Kieren Johnstone
  • 41,277
  • 16
  • 94
  • 144
4

You need to listen on a specific port.

By passing in zero, you are assigned an arbitrary port, so you will receive only UDP datagrams addressed to it. In other words, you will receive nothing.

If you did receive something, the IPEndPoint would be filled in with information about the sender. The initial value could be used to constrain the sender.

Steven Sudit
  • 19,391
  • 1
  • 51
  • 53