CodeSnip: Resolving Domain Names and IP Addresses
page 1 of 1
Published: 09 Dec 2005
Unedited - Community Contributed
Using the Dns class in System.Net namespace in .NET, we can easily resolve domain names and IP addresses. In this CodeSnip, Rajesh gives us a sweet, simple sample showing how to do this.
by Rajesh Toleti
Average Rating: 
Views (Total / Last 10 Days): 14200/ 18


To retrieve information about a specific host from the Internet Domain Name System (DNS), we have to use Dns, IPHostEntry classes, which are in System.Net name space, in .Net framework. We discuss Resolve method of this class with an example.

The Theory

The Resolve method in Dns class resolves a DNS host name or IP address to an IPHostEntry class instance. The IPHostEntry class has three properties.

1) HostName: It contains the primary host name for a server.

2) AddressList: It contains an array of IP addresses associated with a host.

3) Aliases: It contains an array of DNS names associated with a host.

The Code

We will inspect the above concepts with the following code.

Listing 1: Resolvedomain.aspx (VB.NET)

<%@ Page Language="VB" Debug="true" %>

<%@ Import Namespace="System.Net" %>


<Script runat="Server">

Sub Button_Click( s As Object, e As EventArgs )

      Dim objIPHostEntry As IPHostEntry

      Dim i As Integer


            objIPHostEntry = Dns.Resolve( trim(txtDomain) )


            For i = 0 To objIPHostEntry.AddressList.Length - 1


        Next i

        For i = 0 To objIPHostEntry.Aliases.Length - 1

                  lbl3.Text += objIPHostEntry.Aliases(i).ToString()&"<BR>"

            Next i     

      Catch ex as Exception


      End Try

End Sub


Listing 2: ResolvedomainCS.aspx (C#)

<%@ Page Language="c#" Debug="true" %>

<%@ Import Namespace="System.Net" %>


<Script runat="Server">

private void Button_Click(object sender,System.EventArgs e)


 IPHostEntry objIPHostEntry;

 int i;


    objIPHostEntry = Dns.Resolve(txtDomain);


    for(i = 0;i< objIPHostEntry.AddressList.Length;i++){



    for(i = 0;i< objIPHostEntry.Aliases.Length;i++){




      }catch (Exception ex){





Listing 3:HTML Code




<form id="Form1" runat="Server">

  <font face="Verdana, Arial, Helvetica, sans-serif" color="#009900">Enter: </font>

  <font color="#009900"><asp:TextBox


  Runat="Server" /> <asp:Button



  Runat="Server" /> </font><br>

  <font face="Verdana, Arial, Helvetica, sans-serif" color="Red">




  Runat="Server" /></font><br>

  <p> <font face="Verdana, Arial, Helvetica, sans-serif" color="#009900">

    Primary host name for the server:</font><br>





  Runat="Server" /><br>

  <p> <font face="Verdana, Arial, Helvetica, sans-serif" color="#009900">

    IP Addresses:</font><br>




  Runat="Server" />

  <p> <font face="Verdana, Arial, Helvetica, sans-serif" color="#009900">





  Runat="Server" />





You have to combine Listing1 (if you write code in VB.NET) or Listing2 (if you think C# is great!) with Listing3 to make a complete application. Let us have a look on the code (Listing 1 or Listing 2). We import the System.Net namespace to incorporate Dns and IPHostEntry classes in our application. We packed all our code in the Button_Click subroutine (void function in C#), which is called when you click the submit button. In the above subroutine, we created the instance of the IPHostEntry class. We assigned the properties of that instance by calling the Resolve method of the Dns class. The Resolve method takes the Domain name or IP address value as the argument.

Then we assigned the HostName property of the object objIPHostEntry to the label lbl1. We assigned the AddressList property to the label lbl2 and iterated through all the values in the array. In the same way we assigned aliases to the label lbl3. We used a TryCatch block in case we hit a brick wall. If it happens we assign the error message to the label lbl4.

The Example

If we enter the domain name in the text box, we will get the following information (as shown in the screen shot). If we give an IP address, for example, we will get a domain name, Every IP address may not have a website address as a primary host name, or the IP address may itself represent a primary host name.



For any valid domain name there should be at least one primary host name and one IP address associated with it. Some domains may have more than one alias and more than one IP address associated with it, as in the above example. Some domain names may not have aliases at all. If you put the code listed above together, youll have a quick sample showing how to resolve domain names and IP addresses using the System.Net namespace. Enjoy!




User Comments

Title: ebookalliance   
Date: 2011-05-05 7:58:27 PM
Cannot connect to website above. I was connected last night. Is the website temporarily down?
Title: In ASP .NET How to Get IP address from Domain name server by hostname   
Name: muruganad
Date: 2009-10-27 6:49:13 AM
Please click the url for the solution


Murugan Andezuthu Dharmaratnam
Title: Answer of many new students of .net environment   
Name: Dilip Kumar Vishwkarma
Date: 2007-09-19 3:00:48 AM
From the given code we can extract email address's and url's in specified web page. If u have any suggestion plz reply to my email address
Title: problem with this resolve method   
Date: 2007-06-14 7:34:40 AM
the c# code which was given is working fine but, if i have
given an url which is existing but is temporarily showing a
message server not found
please try this

please reply me at
Title: starting Body Tag Missing   
Name: bals
Date: 2006-02-28 5:01:04 AM
The starting body tag is not present in the HTML code
Title: I have better code then it   
Name: Asghar Malik
Date: 2005-12-31 3:59:54 PM
I think the code u have given its some little confunsed lets see my code.
String hostname =DNS.GetHostName();

IPHostEntry ipadd =DNS.GetHostByName(hostname);
foreach (IPAddress address in ipadd.Addresslist());
Title: Good   
Name: Asghar Malik
Date: 2005-12-31 3:49:00 PM
Its a good aricle and code to help and learn
Title: Excelant   
Name: Qaisar
Date: 2005-12-30 5:17:00 AM
Its very helpfull
Title: Code Error C#   
Name: Kapil
Date: 2005-12-29 1:56:59 AM
objIPHostEntry = Dns.Resolve(txtDomain);
should be
objIPHostEntry = Dns.Resolve(txtDomain.Text);

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-05-23 6:12:04 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search