Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / January 2008

Tip: Looking for answers? Try searching our database.

Active host IP address

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Neil - 29 Dec 2007 20:17 GMT
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
Peter Duniho - 29 Dec 2007 23:17 GMT
> [...]
> 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
Neil - 30 Dec 2007 10:12 GMT
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

>> [...]
>> But how do I determine which of these addresses belongs to the active
[quoted text clipped - 10 lines]
>
> Pete
Peter Duniho - 31 Dec 2007 00:23 GMT
> 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
Charles Wang[MSFT] - 02 Jan 2008 10:19 GMT
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.
======================================================
Charles Wang[MSFT] - 04 Jan 2008 09:54 GMT
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.
======================================================

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.