A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Get the IP address in a Windows application
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.
On Thursday, October 27th 2005 at 11:39 AM
By Andrew Pociu (View Profile)
(Rated 4.7 with 45 votes)
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:
Here's how in .NET 1.1:
In .NET 2.0 GetHostByName is obsolete, and was replaced by GetHostEntry:
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:
and in .NET 2.0:
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:
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:
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:
While in PHP this will work great:
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 22.214.171.124 or whatever your IP address is. Now we can parse this page from our Windows application and get the IP address:
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.
|Digg It! Del.icio.us Reddit StumbleIt Newsvine Furl BlinkList|
Rate this tutorial
There are no related tutorials.
Related Source Code
There is no related source code.
C# Job Search