Learn 3 different methods of retrieving the IP address of the computer in a Windows application. It shows you how to enumerate network (local) IP addresses and the (external) Internet IPs. Examples are in C# but can easily be converted to VB.NET.
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”); 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.