Obtain all IP addresses of local machine


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 ( 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>

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 + “: ” +


Download demo project and source – 5.55 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read