November 26th, 2008
Converting an IP address to a Hostname, and back with C# - 2

If you are using TCP/IP connections in your C# application you will need to look up DNS addresses. The following article shows how you can convert an IP address to a hostname, and a hostname to an IP address. Other common uses are to find the hostname address of the local machine, and all its IP addresses — each of which we will show in the examples below.
For this to work we need to include the DNS class which is part of System.Net.
As shown in the following example Dns.GetHostEntry is the function that does all the work. It is available from .NET 2.0 and depreciates the earlier DNS.GetHostByName and DNS.Resolve methods.
We use google.com in the following example to demonstrate that a single domain name can map to more than one IP address:
Example 1: Lookup a DNS entry (name to IP)
// ---------------------------------------------------------------------------------
// Example #1
// Map a domain name to a set of IP addresses
String HostName = "www.google.com";
Console.WriteLine("Looking up: {0}", HostName);
IPHostEntry NameToIpAddress;
NameToIpAddress = Dns.GetHostEntry(HostName);
int AddressCount = 0;
foreach (IPAddress Address in NameToIpAddress.AddressList)
Console.WriteLine("IP Address {0}: {1}", ++AddressCount, Address.ToString());
Example 2: Reverse DNS lookup
Using one of the Google IP addresses I gleaned from the above example we do a reverse DNS lookup:
// ---------------------------------------------------------------------------------
// Example #2
//
// Map an IP address to a hostname
IPHostEntry IpToDomainName = Dns.GetHostEntry("209.85.173.99");
Console.WriteLine("DomainName: {0}", IpToDomainName.HostName);
Example 3: Look up the local hostname
If you would like to know the local hostname of the machine your application is running we can find out as follows:
Note: Because the below call can throw a SocketException (if something is wrong with the Network/ Internet setup on the machine) we need to include System.Net.Sockets to be able to catch it.
// ---------------------------------------------------------------------------------
// Example 3
//
// Lookup the machine's hostname
String LocalHostName;
try
{
LocalHostName = Dns.GetHostName();
Console.WriteLine("Hostname: {0}", LocalHostName);
}
catch (SocketException e)
{
Console.WriteLine("SocketException caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
Example 4: List all IP addresses for the local host
To obtain an array with all the IP addresses of the local machine we can call either the Dns.GetHostEntry or the Dns.GetHostAddresses method with an empty string as a parameter. In the below example we use Dns.GetHostEntry.
// ---------------------------------------------------------------------------------
// Example 4
//
// List all the IP Addresses for the local host ("" refers to the local host)
IPHostEntry LocalEntry = Dns.GetHostEntry("");
AddressCount = 0;
foreach (IPAddress Address in LocalEntry.AddressList)
Console.WriteLine("IP Address {0}: {1}", ++AddressCount, Address.ToString());
Some last notes
It should be noted that although the C# DNS class offers quite a lot of methods they all do basically the same (Hostname to IP, IP to Hostname). The .NET framework does not currently provide any functionality for looking up MX (mail exchange) records, discover nameserver or other common DNS functions.
Image credit: Rich Anderson
Tags: dns, hostname, ip address, Learn C#









Except where otherwise noted, content on this site is
March 9th, 2009 at 1:02 am
for 2 days i am looking for this simple line tnx a lot
February 3rd, 2010 at 3:55 pm
Example 2: Reverse DNS lookup also returning back the ip address
Please use the following method, its working fine
public static string getDNSName(string myIP)
{
System.Net.IPHostEntry ip = System.Net.Dns.GetHostByAddress(myIP);
return ip.HostName;
}