In this blog I will tell you that how to display all IP addresses for given host name. Suppose that we want to know that how many IP addresses are allocated to for Google. Then for performing this task I will take help of DNS class which resides in System.Net package. DNS class provides some use full method through which we will display all IP addresses of given host name.
Example to retrieve IP address of given host name
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Net; //This namespace is required for given host name.
namespace DatabaseProgramingDemo1
{
class Program
{
static void Main(string[] args)
{
Program pr = new Program();
pr.displayIpAddress("www.google.com"); //Pass host name for which you want to IP address.
Console.ReadLine();
}
public void displayIpAddress(string hostName)
{
try
{
//Call resolve method of Dns class which have a property named AddressList which will return IP
//Address array/
IPAddress[] ipAddressArr = Dns.Resolve(hostName).AddressList;
//Iterate all IP addess and display their information.
foreach (IPAddress address in ipAddressArr)
{
Console.WriteLine("{0}/{1}", hostName, address); //Display address value and host name.
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
Output of following code snippet is as follows
www.google.com/209.85.175.106
www.google.com/209.85.175.147
www.google.com/209.85.175.99
www.google.com/209.85.175.103
www.google.com/209.85.175.104
www.google.com/209.85.175.105
Anonymous User
06-May-2019Thanks for sharing.
Sushant Mishra
17-Jul-2017I am really helpful to you, for sharing a post like this.