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 can't instantiate the class before using it because all of its members are static.

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

 IPHostEntry HostInfo = Dns.GetHostByName( "www.somedomain.com" ); 

The IPHostEntry object contains all the information that DNS can obtain about a given host. This object 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. It is rare to have more than one IP address for a domain, but it is possible that's why under most circumstances you'll just use the first 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 can also 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 the address 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( "208.242.41.232" ); 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 13.1.

Figure 13.1. The DNS Demo Shows How to Do DNS Lookups and Reverse DNS Lookups.

graphics/13fig01.jpg

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

Listing 13.1 The Source Code for the DNS Demo Portion of the Networking Demo Application
 private void Page_Load(object sender, System.EventArgs e) {   try   {     HostName.Text = Dns.GetHostName();     IPHostEntry Host = Dns.GetHostByName( HostName.Text );     IPAddress.Text=Host.AddressList[0].ToString();   }   catch   {   } } 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.Get HostName() 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 IPHostEntry 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 named IPAddress in the .aspx file. 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 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 13.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 something such as 208.242.41.135, this program goes out and finds the domain name of that IP address. It does so by calling the Dns.Get HostByAddress() 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. The Dns class supports synchronous and asynchronous methods of retrieving data. The synchronous methods are as easy to use as the Dns class methods that we have already discussed, and the asynchronous methods are easy as long as you understand .NET threading and synchronization.



ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ASP. NET Solutions - 24 Case Studies. Best Practices for Developers
ISBN: 321159659
EAN: N/A
Year: 2003
Pages: 175

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net