Description





Description

The Dns class creates and sends queries to obtain information about a host server from the Internet Domain Name System (DNS). In order to access DNS, the machine executing the query is required to be connected to a network. If the query is executed on a machine that does not have access to a domain name server, a System.Net.Sockets.SocketException is thrown.

Information from the DNS query is returned in an instance of the IPHostEntry class. If the specified host has more than one entry in the DNS database, the IPHostEntry instance contains multiple IP addresses and aliases.

[Note: See the IPHostEntry class page for an example that uses the Dns class.]

Example

using System;

using System.Net;





public class DnsSample

{

    private static bool bDone = false;



    public static void Main()

    {

        String toFind = "microsoft.com";

        IAsyncResult dummy = Dns.BeginResolve(toFind,

                     new AsyncCallback(DnsCallback), null);

        while(!bDone) {}

    }



    private static void DnsCallback(IAsyncResult ar)

    {

        IPHostEntry host = Dns.EndResolve(ar);

        ShowHostDetails(host);

        bDone = true;

    }



    private static void ShowHostDetails(IPHostEntry host)

    {

        Console.WriteLine("HostName = '{0}'", host.HostName);

        foreach (IPAddress addr in host.AddressList)

        {

            Console.WriteLine("IPAddress = {0}", addr);

        }

        foreach (String alias in host.Aliases)

        {

            Console.WriteLine("Alias = {0}", alias);

        }

    }



}


The output is


HostName = 'microsoft.com'

IPAddress = 207.46.130.108

IPAddress = 207.46.250.119



     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows