DNS Class in the System.Net namespace. You can resolve Domain Names and IP Addresses and much more.

DNS Class in the System.Net namespace. You can resolve Domain Names and IP Addresses and much more. The .NET Framework class library provides network programming functionality in the System.Net and System.Net.Sockets namespaces. The System.Net namespace has lot of very useful network related stuff that can ease lot of our difficulties. DNS class of this Namespace is one such example. We can use this to resolve IP addresses and Domains. The DNS class can also be used to retrieve informati

The DNS class is a static class that allows you to easily look up a specific host from the Internet Domain Name System (DNS). It requires Internet connectivity to properly function. The host information a DNS query retrieves is returned as an IPHostEntry object. If the specified host has more than one entry in the DNS database, the IPHostEntry object contains multiple IP addresses and aliases. You may use a for loop to access the entire address.

The Resolve method in DNS class resolves a DNS host name or IP address and returns an instance of IPHostEntry class. 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.


IPHostEntry oIPHostEntry = Dns.Resolve("www.eggHeadCafe.com")
string hostName  = oIPHostEntry.HostName;

  string adresses = string.Empty;

  for(i = 0;i< oIPHostEntry.AddressList.Length;i++){
            adresses+=oIPHostEntry.AddressList[i].ToString() + " ";
  string aliases = string.Emtpy;  
  for(i = 0;i< oIPHostEntry.Aliases.Length;i++){
             aliases+=oIPHostEntry.Aliases[i].ToString()+" ";

Please note:

1. Every IP address need not necessarily have an address as a primary host name, or the IP address may itself be a primary host name.
2. For a given valid domain , there should be at least one primary host name and atleast one IP address associated with it.
3. Some domain names may not have aliases at all.

Other examples in this class is retrieving the hostname as follows.

    //Retrieves the hostname
    string hostName = Dns.GetHostName();
    IPHostEntry adresses = Dns.GetHostByName(hostName);
    foreach (IPAddress address in adresses.AddressList)
By [)ia6l0 iii   Popularity  (2372 Views)