Home About

November 26th, 2008

Converting an IP address to a Hostname, and back with C# - 1

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("");
            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;

                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

Be Sociable, Share!

Tags: , , ,

One Response to “Converting an IP address to a Hostname, and back with C#”

  1. doron Says:

    for 2 days i am looking for this simple line tnx a lot

Most popular
Recent Comments
  • ARS: great plugin! I love it! but, it will be so nice if you can add attribute ‘title’ as one of...
  • Nelson: Saved me from doing it myself. Good article.
  • andy: i am currently playing taiwanese server wow in 奈辛瓦里(PVP) and i would like to realm transfer to somewhere there...
  • berties: any english speaking playing on a taiwanese server?
  • web application development: has C# search volume really so constant over the years? really surprising.