Obtain all IP addresses of local machine

.



Click here for larger image

Environment: Compiled on: Visual Studio .NET & Windows XP Pro. Tested on Win2K Server + WinXP Pro.

I recently implemented a C++, non MFC version of finding your local IP addresses. As it turns out, it was mistaken for C#. I decided to quickly code up a C# example of the exact same thing.

It has been tested in a Windows XP Professional environment, along with a Windows 2000 Server environment. I enumerated all the IPs using a foreach loop instead of NULL termination as in C++. It should have no problems working with Windows 2K/XP and any other C# environment. Tested with up to 32 IPs.

Why would you want to enumerate the IP addresses on the local machine? Well the key problem is that when you are writing sockets applications, a computer can have multiple IP addresses with one NIC and sometimes you don't want to bind to all of them. We have numerous servers with at least 32 IPs each, with multiple NICs. We have services running on port 80 and would like to be able to run IIS on port 80 as well. This requires that our services do not bind to all the existing IPs. This also requires IIS not to bind to all the existing IPs.

The way to accomplish this is to enumerate the IPs on the machine and bind to the IP that is desirable to you. Obviously there are different routing abilities of different IP address, depending on the subnet they belong to. But in essence, you do not want to bind to all IPs because then you cannot have different services listening on the same port on a single NT box. For example, IIS by default has socket pooling and this causes it to bind to ALL IP addresses. We had to disable socket pooling before we could install our services.

In another scenario, you may need to bind to one of the IPs because you are writing a listening socket and rather than bind to all the IPs (0.0.0.0) you want to bind to the one connected to the internet, so that the intranet NIC does not have a listening socket on that port. In this example we have two NICs and multiple IP addresses per NIC. In the above example we have one NIC and 32 IPs on that one NIC. Even though our servers have multiple NICs AND multiple IPs.

In any case, there is a standard method of enumerating the IPs, and I ported the C++ program to C# (basically re-wrote it), this morning.

Finally you might want to note that the executable in C++ without MFC is 36.9k and the executable in C# is 5.6k. Although I've noticed more latency when executing the C# application, probably because its reliance on a whole lot of .NET DLLs which needed to be loaded up into RAM.

It might be interesting for you to check out the C++ version of this GetIP program and compare it to the C# version. You can find it on CodeGuru.com.

If you have any questions, don't hestitate to e-mail me.

using System;
using System.Net;

namespace GetIPCS
{
 /// <summary>
 /// Gets IP addresses of the local machine
 /// </summary>
 class classGetIPCS
 {
  /// <summary>
  /// Gets IP addresses of the local machine
  /// </summary>
  [STAThread]
  static void Main(string[] args)
  {
   // Get host name
   String strHostName = Dns.GetHostName();
   Console.WriteLine("Host Name: " + strHostName);

   // Find host by name
   IPHostEntry iphostentry = Dns.GetHostByName(strHostName);

   // Enumerate IP addresses
   int nIP = 0;
   foreach(IPAddress ipaddress in iphostentry.AddressList)
   {
    Console.WriteLine("IP #" + ++nIP + ": " +
                      ipaddress.ToString());
   }
  }
 }
}

Downloads

Download demo project and source - 5.55 Kb


Comments

  • Thanks

    Posted by Anon on 10/18/2012 04:43pm

    Thanks a lot, this is just what I was looking for!

    Reply
  • help changing IP

    Posted by melisa on 09/08/2012 06:21pm

    Hello, I can see that you are a genius with all this computer language, and I need to ask you as an expert and advice in my problem: I was suspended from ebay because I had differents accounts and they catch me for my IP. Now I want to see if there is any way that I can open for example 10 accounts en ebay and manually change the IPs in my computer every time that I login in ebay for the diferent accounts, is that possible? Your help will save my life, thank you in advance.

    Reply
  • How can get IP address in

    Posted by amit on 04/10/2012 12:34pm

    http://msdnonline.in/aspdotnet/How-we-can-get-IP-address-in.aspx

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds