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());

            }

        }

转载于:https://www.cnblogs.com/Jiansong/archive/2010/03/22/1691732.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值