Using the Dns Class


Using the Dns Class

The Internet Domain Name System (DNS) is the general system with which networking applications can retrieve information about specific hosts on a network, usually on the Internet. This information includes host names, domain names , and IP addresses. The Dns class that comes with the .NET framework is a static class. You do not need to instantiate anything before you use it; you can simply call the static methods that are a part of the class.

When the Dns methods return information, it is always in the form of an IPHostEntry object. For example, the following line gets host information based on a domain name:

 IPHostEntry HostInfo = Dns.GetHostByName( "" ); 

The IPHostEntry object contains all the information that DNS can obtain about a given host. It includes a list of addresses (because any given host can have more than one address), aliases, and the host name. After you get the IPHostEntry object, you should use the first IP address in the list. This is the most common thing you will do with an IPHostEntry object. The most useful way to get an IP address is in the form of an unsigned integer (32 bits). The following code shows how to retrieve the first IP address from an IPHostEntry object that has an unsigned 32-bit integer:

 uint nIPAddress = Convert.ToUInt32( HostInfo.AddressList[0].Address ); 

To represent an IP address in a string, you must convert each of the bytes in the 32-bit integer to a value that will be placed in a string variable. The following code shows how to set a string to reflect an IP address that is contained in an unsigned integer:

 string strSomeString = Convert.ToString( nIPAddress & 0x000000ff ) + "." +      Convert.ToString( ( nIPAddress & 0x0000ff00 ) >> 8 ) + "." +      Convert.ToString( ( nIPAddress & 0x00ff0000 ) >> 16 ) + "." +      Convert.ToString( ( nIPAddress & 0xff000000 ) >> 24 ); 

You also can use the IPAddress class to directly output an IP address as a string with the ToString() method as follows :

 String strSomeString = myIPAddress.ToString(); 

You have already seen how to get an IP address from a domain name. You can do the reverse as well. You can get a host name from an IP address, because DNS does a reverse lookup on an IP address and resolves it to a domain name. The following code shows how to retrieve a domain name from an IP address. Note that the first thing that happens is that an IPHostEntry object is created. This object will contain the host name that the method returns.

 IPHostEntry HostInfo = Dns.GetHostByAddress( "" );  string strSomeString = HostInfo.HostName; 

I have created an application to demonstrate the topics in this chapter. The name of the application is NetworkingDemoApplication. I have created individual pages to demonstrate the individual topics. And to show you the Dns class in action, I have created a page named DnsDemos. When the page first executes, it retrieves the host name and IP address of the current machine. It also gives users the opportunity to type in a domain name and do a resolution, and in another text box users can type in an IP address and get a reverse resolution. This application can be seen in Figure 26.1.

Figure 26.1. The DNS demo shows how to do DNS lookups and reverse DNS lookups.


The source code for the DNSDemos page can be seen in Listing 26.1. Actually, this is not the .aspx source code, but the C# code that is behind the .aspx file.

Listing 26.1 The Source Code for the DNS Demo Portion of the Networking Demo Application
 private void Page_Load(object sender, System.EventArgs e)  {      HostName.Text = Dns.GetHostName();      IPHostEntry Host = Dns.GetHostByName( HostName.Text );      IPAddress.Text=Host.AddressList[0].ToString();  }  private void Button1_Click(object sender, System.EventArgs e)  {      try      {          IPHostEntry Host = Dns.GetHostByName( DomainNameIn.Text );          IPFromDomain.Text=Host.AddressList[0].ToString();      }      catch( Exception ex )      {          IPFromDomain.Text = ex.Message.ToString();      }  }  private void Button2_Click(object sender, System.EventArgs e)  {      try      {          IPHostEntry Host = Dns.GetHostByAddress( IPAddressIn.Text );          DomainFromIP.Text = Host.HostName;      }      catch( Exception ex )      {          DomainFromIP.Text = ex.Message.ToString();      }  }  private void Button3_Click(object sender, System.EventArgs e)  {      Response.Redirect( "MainMenu..aspx" );  } 

When you look at the source code, notice the Page_Load() method. In the Page_Load() method, the first thing that happens is that the Dns.GetHostName() method is called. This method returns the host name of the server on which the application is running. The next line of code uses this name, the host name, to retrieve an IP host entry object in which the IP address is contained.

The second line of code in the Page_Load() method calls the Dns.GetHostByName() method, whereas the third line in the Page_Load() method retrieves the first IP address from the IPHostEntry object as an unsigned integer. The last thing that happens in the Page_Load() method is that the 32-bit integer is converted into a string, and the string represents the IP address of the server. This string will be stored into a label in the .aspx file named IPAddress. The user can then view the IP address.

The Button_Click() method is fired when the user wants to perform a lookup on a domain name. In other words, the user types in a domain name, and this code looks up that domain name's IP address. The code first calls the Dns.GetHostByName() method, which returns an IPHostEntry object. The next thing it does is to retrieve the unsigned 32-bit integer of the first IP address. Finally, it converts the 32-bit IP address into a string value, which is then easily read by the user. You might notice, too, that any exceptions that are thrown are caught in the error message displayed on the screen.

The last method in the code in Listing 26.1 is the Button2_Click() method. This method fires when the user wants to do a reverse lookup on an IP address. In other words, if in the second editable text field the user types in something such as, this program goes out and finds the domain name of that IP address. It does so by calling the Dns.GetHostByAddress() method, which returns an IPHostEntry object. The IPHostEntry object has a property named HostName that contains the host name that resulted from the reverse lookup.

The Dns class has several other useful methods ”most notably, the ones that enable you to do asynchronous lookups. Because this book does not cover the C# and VB languages in great detail, we have not covered issues of threading and thread synchronization. For that reason, it is impractical to cover these additional methods at this time. But they are as easy to use as the Dns class methods that we have already discussed, as long as you understand .NET threading and synchronization.


Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233 © 2008-2017.
If you may any questions please contact us: