15.3 Resolving an IP Address or Hostname Using DNS

 <  Day Day Up  >  

You want to resolve a hostname or an IP address using the Domain Name System ( DNS ).


Technique

The DNS class contains a collection of static methods designed to resolve an IP address or a hostname using DNS. Each of the methods returns an IPHostEntry object containing the IP address of the host computer being searched for. The DNS.GetHostByName method searches for the given host given the friendly string name of an IP address such as www.samspublishing.com. DNS.GetHostByAddress uses a string or an IPAddress object to perform a lookup. This method and GetHostByName return a null value if the specified host is not found, which makes them a good candidate to ensure the server you want to connect to is operational before you connect to it.

The last method is a combination of the two methods just mentioned. The DNS.Resolve method accepts a string parameter that can denote either a hostname or an IP address. The Resolve method determines which scheme is being specified and resolves the hostname accordingly . The following code first checks for any command-line arguments specifying the server to connect to, and if none are present, it uses the localhost IP address. The DNS.Resolve method creates an IPHostEntry object that will be used to connect to the remote computer, as shown in Listing 15.4.

Listing 15.4 Performing DNS Lookup
 [STAThread] static void Main(string[] args) {     TcpClient client = new TcpClient();     Byte[] read = new Byte[1024];     string gameServer = "";     if (args.Length != 1)     {         gameServer = "127.0.0.1";     }     else     {         gameServer = args[0];     }     // Verify that the server exists     IPHostEntry serverIP = Dns.Resolve( gameServer );     if( serverIP == null )     {         Console.WriteLine("Cannot find server: {0}", gameServer);         return;     }     else     {         Console.WriteLine( "Found server. {0}", serverIP.HostName );     } } 

Comments

DNS was designed to associate a friendly string value with a numerical IP address. For instance, whenever you navigate to a Web address within a Web browser, the browser itself contacts a DNS server to translate the Web address to the numerical IP address before it makes a connection to that address.

You can also use the DNS class itself and its static methods to effectively detect whether an active Internet connection has already been established. You do so simply by resolving a well-known hostname. If the DNS method is able to successfully resolve that hostname, then the machine currently has an active Internet connection. However, you must ensure that you don't inadvertently receive a false negative, which can occur if the hostname that you are trying to resolve is currently disconnected. Therefore, attempt to resolve a group of different hostnames because the probability that all of them would be down at the same time is pretty low. If you are able to resolve at least one of them, then an Internet connection is active. Also, note that there are many different ways to determine an active Internet connection. The DNS lookup method is a reliable method, but you can also use the Internet Control Message Protocol (ICMP) to perform server pings or use PInvoke , shown in Recipe 21.11, "Calling Native Unmanaged Code Using PInvoke," to call the InternetGetConnectedState method defined in the wininet.dll library.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

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