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!