HighTechTalks DotNet Forums  

Active host IP address

Dotnet Framework microsoft.public.dotnet.framework


Discuss Active host IP address in the Dotnet Framework forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Neil
 
Posts: n/a

Default Active host IP address - 12-29-2007 , 02:17 PM






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


Reply With Quote
  #2  
Old   
Peter Duniho
 
Posts: n/a

Default Re: Active host IP address - 12-29-2007 , 05:17 PM






On Sat, 29 Dec 2007 12:17:06 -0800, Neil <wassoft (AT) newsgroup (DOT) nospam> wrote:

Quote:
[...]
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


Reply With Quote
  #3  
Old   
Neil
 
Posts: n/a

Default Re: Active host IP address - 12-30-2007 , 04:12 AM



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" <NpOeStPeAdM (AT) nnowslpianmk (DOT) com> wrote

Quote:
On Sat, 29 Dec 2007 12:17:06 -0800, Neil <wassoft (AT) newsgroup (DOT) nospam> wrote:

[...]
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



Reply With Quote
  #4  
Old   
Peter Duniho
 
Posts: n/a

Default Re: Active host IP address - 12-30-2007 , 06:23 PM



On Sun, 30 Dec 2007 02:12:08 -0800, Neil <wassoft (AT) newsgroup (DOT) nospam> wrote:

Quote:
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.

Quote:
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.

Quote:
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


Reply With Quote
  #5  
Old   
Charles Wang[MSFT]
 
Posts: n/a

Default Re: Active host IP address - 01-02-2008 , 04:19 AM



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/Sim...?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.
================================================== ====


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.