Question

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 = 169.254.0.27, nic2 = 169.254.0.28, nic3 = 169.254.0.29

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);
    doStuff(receivedBytes);
}

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.

Was it helpful?

Solution

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("192.168.1.2"), 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.

OTHER TIPS

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());
    }
    Console.WriteLine("");
  }

Then you can choose on which address start listening.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top