Active host IP address

  • Thread starter Thread starter Neil
  • Start date Start date
N

Neil

Hi,
I have a c# app that binds to a port on the host machine.
How do I determine the IP address of the active network interface?

On a PC with multiple NICs I can obtain a list of IP addresses as follows:-

string hostName = Dns.GetHostName();
IPHostEntry local = Dns.GetHostByName(hostName);
foreach (IPAddress ipaddress in local.AddressList)
{
Console.WriteLine(ipaddress .ToString());
}

This gives me a list of the available IP addresses on the host available on
all the network interfaces.
But how do I determine which of these addresses belongs to the active
netwrok interface?

Thanks
Neil
 
[...]
But how do I determine which of these addresses belongs to the active
netwrok interface?

What do you mean by "active network interface"?

Any valid address may be considered "active".

If you want your socket (I'm assuming you're using sockets here) bound to
a specific IP address, then just provide that address when you bind the
socket. In that way, you can make the "active" NIC whichever one you
want, assuming you feel a need to.

Pete
 
Hi Pete,
Thanks for your reply.

I have a ethernet NIC and a wireless NIC.
I need to bind to the ethernet NIC specifically, which might have IP
192.168.0.3, the ethernet NIC might have an IP of 192.168.0.5.

By active network interface, I mean the interface currently receiving
packets. So, I need to know which of the two interfaces is being used to
receive packets so that I can bind the socket to the correct IP.


All of this is being used in a packet sniffing app we are developing.

Thanks
Neil

Peter Duniho said:
[...]
But how do I determine which of these addresses belongs to the active
netwrok interface?

What do you mean by "active network interface"?

Any valid address may be considered "active".

If you want your socket (I'm assuming you're using sockets here) bound to
a specific IP address, then just provide that address when you bind the
socket. In that way, you can make the "active" NIC whichever one you
want, assuming you feel a need to.

Pete
 
I have a ethernet NIC and a wireless NIC.
I need to bind to the ethernet NIC specifically, which might have IP
192.168.0.3, the ethernet NIC might have an IP of 192.168.0.5.

Differentiating between a wireless and wired NIC is not the same as
differentiating between an "active" and an "inactive" NIC.
By active network interface, I mean the interface currently receiving
packets. So, I need to know which of the two interfaces is being used to
receive packets so that I can bind the socket to the correct IP.

Again, what do you mean by "active"? "Currently receiving" doesn't really
do it. Either NIC, as long as its enabled, could be "currently receiving".

Also, what behavior do you expect to achieve by binding to a specific IP
address? Binding will affect what addresses are valid for sending data to
a socket, but it won't affect which NIC is used to send from that socket.

So far, your definition of "active" seems to be tied to which NIC is
actually receiving data, so I don't see the point in trying to bind to a
specific network card in this case, since by your definition your socket
is already only receiving data on the "active" NIC, and that's the most
you could hope for by binding a socket to a specific address.
All of this is being used in a packet sniffing app we are developing.

You might want to look at Wireshark (was named "Ethereal"). It's an
already-existing network analysis tool. It might make more sense to just
use that rather than do this yourself.

Pete
 
Hi Neil
I agree with Pete's suggestions.
Anyway I would like to add more comments here. Did you mean that the active
NIC referred to a connected NIC in your LAN? If so, you may consider using
Ping class located in the System.Net.NetworkInformation namespace.
You may refer to:
Simple Ping Utility with GUI
http://www.codeproject.com/KB/IP/SimplePingUtilityWithGUI.aspx?df=100&forumi
d=421321&exp=0&select=2117629&tid=2117629

You may also consider using WMI to check if a NIC is enabled or disabled.
Please refer to:
http://www.visualbasicscript.com/m_3384/tm.htm
http://www.codeguru.com/forum/showthread.php?t=381506

This response contains a reference to a third party World Wide Web site.
Microsoft is providing this information as a convenience to you. Microsoft
does not control these sites and has not tested any software or information
found on these sites; therefore, Microsoft cannot make any representations
regarding the quality, safety, or suitability of any software or
information found there. There are inherent dangers in the use of any
software found on the Internet, and Microsoft cautions you to make sure
that you completely understand the risk before retrieving any software from
the Internet.

Please feel free to let us know if you have any other questions or
concerns. Have a nice day!

Best regards,
Charles Wang
Microsoft Online Community Support
=====================================================
When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
======================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
======================================================
 
Hi Neil,
Happy New Year!

I would like to check with you to see if you need further assistance on
this issue. Please feel free to let us know if you have any other questions
or concerns. Have a nice day!

Best regards,
Charles Wang
Microsoft Online Community Support
=====================================================
When responding to posts, please "Reply to Group" via
your newsreader so that others may learn and benefit
from this issue.
======================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
======================================================
 
Back
Top