Take the 2-minute tour ×

I have 3 different network cards each with their individual responsibility. Two of the cards are receiving packets from a similar device (plugged directly into each individual network card) which sends data on the same port. I need to save the packets knowing which device they came from.

Given that I am required to not specify the ip address of the devices sending me the packets, how can I listen on a given network card? I am allowed to specify a static ip address for all 3 nics if needed.

Example: nic1 =, nic2 =, nic3 =

Right now I have this receiving the data from nic1 and nic2 without knowing which device it came from.

var myClient = new UdpClient(2000) //Port is random example

var endPoint = new IPEndPoint(IPAddress.Any, 0):

while (!finished)
    byte[] receivedBytes = myClient.Receive(ref endPoint);

I can't seem to specify the static ip address of the network cards in a manner which will allow me to capture the packets from just one of the devices. How can I separate these packets with only the knowledge that they are coming in on two different network cards?

Thank you.

share|improve this question
guess what happens if you replace IPAddress.Any with an IP address of the two cards? –  Davide Piras Apr 11 '13 at 21:26
@DavidePiras I've tested that, and I still receive packets from both devices. Is there something else I have to specify along with changing the ip there? –  jsmith Apr 11 '13 at 21:28

3 Answers 3

up vote 1 down vote accepted

You're not telling the UdpClient what IP endpoint to listen on. Even if you were to replace IPAddress.Any with the endpoint of your network card, you'd still have the same problem.

If you want to tell the UdpClient to receive packets on a specific network card, you have to specify the IP address of that card in the constructor. Like so:

var listenEndpoint = new IPEndPoint(IPAddress.Parse(""), 2000);
var myClient = new UdpClient(listenEndpoint);

Now, you may ask "What's the ref endPoint part for when I'm calling myClient.Receive(ref endPoint)?" That endpoint is the IP endpoint of the client. I would suggest replacing your code with something like this:

IPEndpoint clientEndpoint = null;

while (!finished)
    var receivedBytes = myClient.Receive(ref clientEndpoint);
    // clientEndpoint is no longer null - it is now populated
    // with the IP address of the client that just sent you data

So now you have two endpoints:

  1. listenEndpoint, passed in through the constructor, specifying the address of the network card you want to listen on.
  2. clientEndpoint, passed in as a ref parameter to Receive(), which will be populated with the client's IP address so you know who is talking to you.
share|improve this answer
Well explained! That was the simple bit I wasn't grasping. Thank you for the help. –  jsmith Apr 11 '13 at 22:27

Check this out this:

  foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
    Console.WriteLine("Name: " + netInterface.Name);
    Console.WriteLine("Description: " + netInterface.Description);
    Console.WriteLine("Addresses: ");
    IPInterfaceProperties ipProps = netInterface.GetIPProperties();
    foreach (UnicastIPAddressInformation addr in ipProps.UnicastAddresses)
      Console.WriteLine(" " + addr.Address.ToString());

Then you can choose on which address start listening.

share|improve this answer
This will simply give me the static ips which I already know. I do know how to access the cards. I don't know how to create a udp client that only listens on one of those cards. :) –  jsmith Apr 11 '13 at 21:29
So binding to selected IP and port is not working, I thought that you could get Ip from this list programmatcaly and use in creating endpoint instead of Any adress. –  Mateusz Apr 11 '13 at 21:50
It doesn't seem to be working how I am currently doing it. I think I am missing something small... –  jsmith Apr 11 '13 at 22:18

look, if you create your IPEndPoint in the following way it must work:

IPHostEntry hostEntry = null;

// Get host related information.
hostEntry = Dns.GetHostEntry(server);

foreach(IPAddress address in hostEntry.AddressList)
  IPEndPoint ipe = new IPEndPoint(address, port);

try to do not pass 0 as port but a valid port number, if you run this code and break the foreach after the first iteration you will have created only 1 IPEndPoint and you can use that one in your call to: myClient.Receive

notice that the UdpClient class has a member calledd Client which is a socket, try to explore the properties of that object as well to find out some details, I have found the code I gave you here: http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx

share|improve this answer

Your Answer


