Get domain information from the Domain Name System in C#

We can use the Dns class creates and sends queries to obtain information about a host server from the Internet Domain Name System.

using System;
using System.Net;
public class DnsSample
{
    private static bool bDone = false;
    public static void Main()
    {
        String toFind = "w3mentor.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);
        }
    }
}

Output:

HostName = ‘w3mentor.com’
IPAddress = 205.186.187.216
IPAddress = 205.186.187.116

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.