Thursday, April 30, 2009

How To Get IP Address Of A Machine

In this post we will try to explore the Dns class which is under System.net namespace and which provides simple domain name resolution functionality. The DNS class in the System.Net namespace can be used to get the hostname of a machine or get the IP address if the hostname is already known. The DNS class provides a simple domain name resolution functionality. The DNS class is a static class that provides access to information from the Internet Domain Name System (DNS). The information returned includes multiple IP addresses and aliases if the host specified has more than one entry in the DNS database. The list is returned as a collection or an array of IPAddress objects. The following section is the code that shows how to obtain the IP address for a given host name.
The function which is used to get the host name is the GetHostName of Dns class. In the code below I have a function named DisplayHostName which is used to get the host name by calling the Dns.GetHostName function. As Dns class is a static class so you can call the function in the Dns class without creating an object of Dns class.

public void DispalyHostName()
{
try {
// Get the local computer host name.
string strHostName = Dns.GetHostName();
Console.WriteLine("Computer name :" + strHostName);
}
catch(SocketException e) {
Console.WriteLine("SocketException caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
catch(Exception e)
{
Console.WriteLine("Exception caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
}

When calling the GetHostName function of Dns class the exception may occurred which is SocketException , when mean that error is encountered when resolving the local host name. For the code above I have remove the static key word from the DispalyHostName function, as I have to call that function in static function which is Main in console application. So here i have remove static key word.
Here is the code you can use to get the IP Address of a machine. The Dns has two overloaded function for the GetHostEntry one which takes the host name as string parameter and second one which take IP address as parameter.

public void GetIPAddess()
{
IPHostEntry IPHostEntryList;
IPHostEntryList = Dns.GetHostEntry(Dns.GetHostName() );
Console.WriteLine("GetHostEntry({0}) returns:", Dns.GetHostName() );

foreach (IPAddress currentIPAddress in IPHostEntryList.AddressList)
Console.WriteLine("IP Address: {0}", currentIPAddress);
}

The GetHostEntry method queries a DNS server for the IP address that is associated with a host name or IP address.When an empty string is passed as the host name, this method returns the IPv4 addresses of the local host.
If the host name could not be found, the SocketException exception is returned with a value of 11001 (Windows Sockets error WSAHOST_NOT_FOUND). This exception can be returned if the DNS server does not respond. This exception can also be returned if the name is not an official host name or alias, or it cannot be found in the database(s) being queried.
The ArgumentException exception is also returned if the hostNameOrAddress parameter contains Any or IPv6Any.
The GetHostEntry method assumes that if an IP literal string is passed in the hostNameOrAddress parameter that the application wants an IPHostEntry instance returned with all of the properties set. These properties include the AddressList, Aliases, and HostName. As a result, the implementation of the GetHostEntry method exhibits the following behavior when an IP string literal is passed:
  1. The method tries to parse the address. If the hostNameOrAddress parameter contains a legal IP string literal, then the first phase succeeds.
  2. A reverse lookup using the IP address of the IP string literal is attempted to obtain the host name. This result is set as the HostName property.
  3. The host name from this reverse lookup is used again to obtain all the possible IP addresses associated with the name and set as the AddressList property.
For an IPv4 string literal, all three steps above may succeed. But it is possible for a stale DNS record for an IPv4 address that actually belongs to a different host to be returned. This may cause step #3 to fail and throw an exception (there is a DNS PTR record for the IPv4 address, but no DNS A record for the IPv4 address).For IPv6, step #2 above may fail, since most IPv6 deployments do not register the reverse (PTR) record for an IPv6 address. So this method may return the string IPv6 literal as the fully-qualified domain (FQDN) host name in the HostName property.
The GetHostAddresses method has different behavior with respect to IP literals. If step #1 above succeeds (it successfully parses as an IP address), that address is immediately returned as the result. There is no attempt at a reverse lookup. (Source) .You can download the source code from here

All and any comments / bugs / suggestions are welcomed!


3 comments:

VENKAT said...

It shows the code for obtaining the Ip-Address details very fast .But without codes also one can find the Ip-Address of any Domain name using the Domain Host Search in the site www.ip-details.com.All the informations very fast.It is also get details of the Ip-Address server location details by using the Ip-Address search

Asim said...

Thanks for you link, the like you have paste here is very informative. It will be nice if you have any idea how to use that link in application web or desktop, I mean is there any web service available for it ??

Murugan Andezuthu Dharmaratnam said...

http://muruganad.com/ASP.NET/ASP_.NET_domain_name_to_ip_resolution_Get_IP_Address_Given_Host_Name.html

Here's the 3 Line code to get IP From Host Name

Dim IPAddress() As System.Net.IPAddress
IPAddress = System.Net.Dns.GetHostAddresses("smtp.mail.yahoo.com")
Dim oSmtpClient As New SmtpClient
oSmtpClient.Host = IPAddress(0).ToString