How to Get the IP address in a Windows application

Retrieving the IP of the visitor of a website is easy using .NET (ASP.NET to be more exact), but how do you retrieve the IP of the client running a Windows application? There are several reasons why you would want to do that, such as sending the IP to a server on the internet for identifying the client. The question is how do you do this in .NET?

Using .NET 1.1 and 2.0 to retrieve the IP address

Well, in .NET 1.1 and 2.0 there are methods in the System.Net namespace that do that. Speaking of System.Net, don’t forget to include the using statement, to avoid the long lines I have below:

using System.Net;

Here’s how in .NET 1.1:

// Get the hostname 

string myHost = System.Net.Dns.GetHostName();

// Show the hostname 

MessageBox.Show(myHost);

// Get the IP from the host name

string myIP = System.Net.Dns.GetHostByName(myHost).AddressList[0].ToString();

// Show the IP 

MessageBox.Show(myIP);

In .NET 2.0 GetHostByName is obsolete, and was replaced by GetHostEntry:

// Get the hostname 

string myHost = System.Net.Dns.GetHostName();

// Show the hostname 

MessageBox.Show(myHost);

// Get the IP from the host name

string myIP = System.Net.Dns.GetHostEntry(myHost).AddressList[0].ToString();

// Show the IP 

MessageBox.Show(myIP);

As you can see here, we only retrieved position 0 of AddressList but if you expect more than one IP on the user’s machine, you better loop through all of them.
Here’s how in .NET 1.1:

// Get the hostname 

string myHost = System.Net.Dns.GetHostName();

System.Net.IPHostEntry myIPs = System.Net.Dns.GetHostByName(myHost);

// Loop through all IP addresses and display each 

foreach(System.Net.IPAddress myIP in myIPs.AddressList)

{

MessageBox.Show(myIP.ToString());

}

and in .NET 2.0:

// Get the hostname 

string myHost = System.Net.Dns.GetHostName();

System.Net.IPHostEntry myIPs = System.Net.Dns.GetHostEntry(myHost);

// Loop through all IP addresses and display each 

foreach(System.Net.IPAddress myIP in myIPs.AddressList)

{

MessageBox.Show(myIP.ToString());

}

Everything’s well and good. Or is it? I don’t know about your workstation, but my connection to the Internet goes through a network with a router, and using the above methods I only retrieved the local IPs on my network (192.168.0.1, 192.168.0.2, 192.168.0.3 and so on).
So the above code works alright, but not if the computer is behind a router. So in this case, let’s have a look
at the second method:

Using WMI to retrieve the IP address

Let’s see how we can retrieve the IP addresses of your network adapters using WMI. First add a reference to System.Management, and use the appropriate using statement:

using System.Management;

Below is the code that retrieves the IP addresses of the network adapters in your computer. It works with .NET 1.1 as well as in .NET 2.0:

// Query for all the enabled network adapters 

ManagementObjectSearcher objSearcher = new ManagementObjectSearcher(“SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = ‘TRUE'”);

ManagementObjectCollection objCollection = objSearcher.Get();
// Loop through all available network interfaces

foreach (ManagementObject obj in objCollection)

{

// List all IP addresses of the current network interface

string[] AddressList = (string[])obj[“IPAddress“];

foreach (string Address in AddressList)

{

MessageBox.Show(Address);

}

}

Depending on your network configuration this may work unlike the first method we used (using System.Net), however this will normally perform slower than the .NET solution, so if both of them work fine, stick with the first one.
What’s that I hear? Neither method worked for retrieving the Internet IP address? Been there, done that. I couldn’t find a solution to retrieving the (external) Internet IP address when the user was behind a router. And the truth is, there is no easy solution behind this. But the important thing is that there is a solution:

Asking a server for your IP address

So how come ASP.NET manages to display the IP address of the client, and we can’t get our own IP address? Well, it’s easy for ASP.NET to do that because when the client connects to the ASP.NET server it doesn’t matter if he is behind a router or not. In this case, the one who’s making the call to the ASP.NET server is really the router, and his IP address is given to the server. I’m not sure if you understand my blabber, but our solution is to ask a server on the internet for our IP address. A simple way to do that is query a page on the Internet that contains only the IP address, without any HTML tags or anything. Let’s see how this looks like.
First, we need to set up that page. It can be an ASP, ASP.NET, PHP or any other type of page.
In ASP.NET you can use this:

<%= Request.ServerVariables(“REMOTE_ADDR”) %>

While in PHP this will work great:

<?php echo $REMOTE_ADDR; ?>

This will create a web page containing only the IP address of the client. You can access the page via Internet Explorer or any other browser and view its source. It will contain 207.46.19.30 or whatever your IP address is. Now we can parse this page from our Windows application and get the IP address:

System.Net.WebClient myWebClient = new System.Net.WebClient();

System.IO.Stream myStream = myWebClient.OpenRead(http://www.geekpedia.com/ip.php&#8221;);

System.IO.StreamReader myStreamReader = new System.IO.StreamReader(myStream);

string myIP = myStreamReader.ReadToEnd();

MessageBox.Show(myIP);

This has to work! If this doesn’t work, you’re practically invisible on the Internet and I’d like to know how you did it.

You probably noticed in the example we’re reading the IP from http://www.geekpedia.com/ip.php which is a one-line PHP script (the one I showed you above). It currently works but I strongly suggest you don’t base your application on this page on Geekpedia.com, since in the next few months we’re going to switch to an ASP.NET 2.0 server and this page won’t work anymore and neither will your application. Create your own ip.php or ip.aspx page on the web so you can be certain the service is always up.

Advertisements

7 responses to this post.

  1. I would appreciate to retire where air conditioning is not needed, as well as a dog park with benches is nearby.

  2. Wow, amazing blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your web site is great, as well as the content!. Thanks For Your article about How to Get the IP address in a Windows application Denno Secqtinstien Foundation .

  3. Internet is a source of information and knowledge. But there are so many sites in net which are nothing but gibberish. But when I visit your site – I felt that – in net there are lots of sites (like this) from where I can get lot of information too. And if I say truly – your site is fantastic. I’ve bookmarked your site in my browser; I hope in future days I’ll get more valuable information from your site.

  4. That may be the end of this report. Right here you will locate some web pages that we assume you will appreciate, just click the hyperlinks.

  5. I cannot thank you enough for the blog post.Really looking forward to read more. Cool.

  6. Wow, marvelous blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your site is great, as well as the content!. Thanks For Your article about How to Get the IP address in a Windows application | Denno Secqtinstien Foundation .

Comments are closed.

%d bloggers like this: