CodeSnip: Resolving Domain Names and IP Addresses
page 1 of 1
Published: 09 Dec 2005
Unedited - Community Contributed
Abstract
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
Feedback
Average Rating: 
Views (Total / Last 10 Days): 39145/ 39

Introduction

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

      Try

            objIPHostEntry = Dns.Resolve( trim(txtDomain) )

            lbl1.Text=objIPHostEntry.HostName

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

                  lbl2.Text+=objIPHostEntry.AddressList(i).ToString()&"<BR>"

        Next i

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

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

            Next i     

      Catch ex as Exception

            lbl4.Text=ex.Message

      End Try

End Sub

</Script>

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;

 try{

    objIPHostEntry = Dns.Resolve(txtDomain);

    lbl1.Text=objIPHostEntry.HostName;

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

            lbl2.Text+=objIPHostEntry.AddressList[i].ToString()+"<BR>";

                                                                                          }

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

            lbl3.Text+=objIPHostEntry.Aliases[i].ToString()+"<BR>";

                                                                                    }

           

      }catch (Exception ex){

            lbl4.Text=ex.Message;

                                           }

}

</Script>

Listing 3:HTML Code

<html>

<head><title>Dns.aspx</title></head>

 

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

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

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

  id="txtDomain"

  Runat="Server" /> <asp:Button

  Text="Submit!"

  OnClick="Button_Click"

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

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

  <asp:Label

  id="lbl4"

  EnableViewState="False"

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

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

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

   

    <asp:Label

  id="lbl1"

  EnableViewState="False"

  Runat="Server" /><br>

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

    IP Addresses:</font><br>

    <asp:Label

  id="lbl2"

  EnableViewState="False"

  Runat="Server" />

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

  Aliases:</font><br>

    <asp:Label

  id="lbl3"

  EnableViewState="False"

  Runat="Server" />

 

</form>

</body>

</html>

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 www.hotmail.com in the text box, we will get the following information (as shown in the screen shot). If we give an IP address, for example 204.176.140.50, we will get a domain name, www.eenadu.net. 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.

 

Conclusion

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   
Name: ebookalliance.net
Date: 2011-05-05 7:58:27 PM
Comment:
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
Comment:
Please click the url for the solution

http://muruganad.com/ASP.NET/ASP-.NET-domain-name-to-ip-resolution-Get-IP-Address-Given-Host-Name.html


Thanks!

Murugan Andezuthu Dharmaratnam
www.muruganad.com
Title: Answer of many new students of .net environment   
Name: Dilip Kumar Vishwkarma
Date: 2007-09-19 3:00:48 AM
Comment:
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 dilip4ever89@yahoo.co.in
Title: problem with this resolve method   
Name: s.harish611@gmail.com
Date: 2007-06-14 7:34:40 AM
Comment:
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 www.completedotnet.com

please reply me at s.harish611@gmail.com
Title: starting Body Tag Missing   
Name: bals
Date: 2006-02-28 5:01:04 AM
Comment:
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
Comment:
I think the code u have given its some little confunsed lets see my code.
String hostname =DNS.GetHostName();
Console.Writeline(hostname);

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






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


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-03-30 10:37:55 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search