IP Addressing and DNS - C#

In this section, we mainly talks about using the Dns class to perform DNS name resolution. The Dns class can perform both forward and reverse name lookup to a DNS server.

Forward name lookup is resolving a name to one or more IP addresses,

Reverse name lookup is resolving an IP address to a name.

From MSDN, we can know that the Dns class is a static class that retrieves information about a specific host from the Internet Domain Name System (DNS). The host 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, IPHostEntry contains multiple IP addresses and aliases.

There are many useful methods that can help us to do the job. It mainly include the synchronous and asynchronous methods as we have talked before about delegate’s asynchronous, but please pay attention that here the beginXXX method and EndXXX methods are Dns class’s static method. Here shows a simple example about asynchronous invoke:

// Signals when the resolve has finished.

public static ManualResetEvent GetHostEntryFinished =

    new ManualResetEvent(false);

 

// Define the state object for the callback.

// Use hostName to correlate calls with the proper result.

public class ResolveState

{

    string hostName;

    IPHostEntry resolvedIPs;

 

    public ResolveState(string host)

    {

        hostName = host;

    }

 

    public IPHostEntry IPs

    {

        get { return resolvedIPs; }

        set {resolvedIPs = value;}}

    public string host {get {return hostName;}

        set {hostName = value;}}

}

 

// Record the IPs in the state object for later use.

public static void GetHostEntryCallback(IAsyncResult ar)

{

    ResolveState ioContext = (ResolveState)ar.AsyncState;

 

    ioContext.IPs = Dns.EndGetHostEntry(ar);

    GetHostEntryFinished.Set();

}      

 

// Determine the Internet Protocol (IP) addresses for

// this host asynchronously.

public static void DoGetHostEntryAsync(string hostname)

{

    GetHostEntryFinished.Reset();

    ResolveState ioContext= new ResolveState(hostname);

 

    Dns.BeginGetHostEntry(ioContext.host,

        new AsyncCallback(GetHostEntryCallback), ioContext);

 

    // Wait here until the resolve completes (the callback

    // calls .Set())

    GetHostEntryFinished.WaitOne();

 

    Console.WriteLine("EndGetHostEntry({0}) returns:", ioContext.host);

 

    foreach (IPAddress ip in ioContext.IPs.AddressList)

    {

        Console.WriteLine("    {0}", ip);

    }

 

}

Another important class is IPHostEntry, which is the type of member method return value. IPHostEntry has three important properties listed below:

AddressList

Gets or sets a list of IP addresses that are associated with a host.

Aliases

Gets or sets a list of aliases that are associated with a host.

HostName

Gets or sets the DNS name of the host.

Here I code an excise to show how do use IPHostEntry and its properties:

static void ResolveName(string name)

        {

            try

            {

                //actually here we can use computer name or IP address

                IPHostEntry ipHost = Dns.GetHostEntry(name);

                //output the HostName

                Console.WriteLine("The host name is: " + ipHost.HostName.ToString());

                //output alias

                if (ipHost.Aliases.Length > 0)

                {

                    Console.WriteLine("Aliases found are: ");

                    foreach (string ali in ipHost.Aliases)

                    {

                        Console.WriteLine(ali);

                    }

                }

                Console.WriteLine("IP address found are: ");

                int ipv4Count = 0;

                int ipv6Count = 0;

                foreach (IPAddress address in ipHost.AddressList)

                {

                    //IPv4 address, the AddressFamily is "InterNetwork"

                    if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)

                    {

                        ipv4Count++;

                        Console.WriteLine("IPV4 Address # " + ipv4Count.ToString() + " is " + address.ToString());

                    }

                    //IPv6 address, the AddressFamily is "InterNetworkV6"

                    else if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)

                    {

                        ipv6Count++;

                        Console.WriteLine("IPV6 Address # " + ipv6Count.ToString() + " is " + address.ToString());

                    }

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.ToString());

            }

        }

posted @ 2010-03-22 16:56  Jiansong  阅读(422)  评论(0编辑  收藏  举报