Support us on YouTube by Subscribing our YouTube Channel. Click here to Subscribe our YouTube Channel

Sunday 20 July 2014

Getting IP Address and Host Name using DNS Class in C#

In general, When we have to find IP Address of a machine, we run ipconfig command in command prompt.


We can find the same with C# using DNS Class. DNS (Domain Name Service) class is present in System.Net namespace that has methods to retrieve IP Address, Host Name etc.

Example: Program for getting Host Name
Firstly, Add System.Net namespace in namespace declaration. GetHostName() method of Dns class returns the host name of the local computer.

string HostName = Dns.GetHostName();
Console.WriteLine("Name of machine ="+HostName);

Preview:

Example: Program For getting IP Address of Host(Local)
Dns class provides GetHostAddresses() method which takes HostName as a parameter and returns an array of IPAddress. We get both IPv4 and IPv6 of the machine.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace GetHostNameAndIPAddress
{
    class Program
    {
        static void Main(string[] args)
        {
            string HostName = Dns.GetHostName();
            Console.WriteLine("Host Name of machine ="+HostName);
            IPAddress[] ipaddress = Dns.GetHostAddresses(HostName);
            Console.WriteLine("IP Address of Machine is");
            foreach(IPAddress ip in ipaddress)
            {
                Console.WriteLine(ip.ToString());
            }
        }
    }
}
Preview:

Example: Program for getting IPAddress of Domain name

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace GetIPusingHostname
{
    class Program
    {
        static void Main(string[] args)
        {
            string HostName = "www.bing.com";
            IPAddress[] ipaddress = Dns.GetHostAddresses(HostName);
            Console.WriteLine("IPAddress of " + HostName + " is");
            foreach (IPAddress ipaddr in ipaddress)
            {
                Console.WriteLine(ipaddr);
            }
        }
    }
}
Preview:

Example: Program for Getting IPv4 and IPv6 Separately by checking the Address Family

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace GetIPv4andIPv6Seperate
{
    class Program
    {
        static void Main(string[] args)
        {
            string HostName = Dns.GetHostName();
            Console.WriteLine("Host Name of machine =" + HostName);
            IPAddress[] ipaddress = Dns.GetHostAddresses(HostName);
            Console.Write("IPv4 of Machine is ");
            foreach (IPAddress ip4 in ipaddress.Where(ip => ip.AddressFamily==System.Net.Sockets.AddressFamily.InterNetwork))
            {
                Console.WriteLine(ip4.ToString());
            }
            Console.Write("IPv6 of Machine is ");
            foreach (IPAddress ip6 in ipaddress.Where(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6))
            {
                Console.WriteLine(ip6.ToString());
            }
        }
    }
}
Preview:

Example: Program for getting Host Name based on IPAddress
getHostEntry() method of DNS class takes IPAddress as parameter and returns IPHostEntry that contains address information about the host specified in address.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace GetHostNameUsingIP
{
    class Program
    {
        static void Main(string[] args)
        {
            string IPAdd = "204.79.197.200";
            IPHostEntry hostEntry = Dns.GetHostEntry(IPAdd);
            Console.WriteLine(hostEntry.HostName);
        }
    }
}
Preview:

Hope you like it. Thanks
[Download Source Code from Google Drive]

0 comments:

Post a Comment

Subscribe us on YouTube

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Like us on Facebook