Tuesday 31 July 2012

.Net Tips, C# Tips : Get IP Address from host name with C# Examples and VB.Net Examples

You can get IP Address of a fully qualified host name. We can Use GetHostEntry method of the System.Net.Dns class. There are multiple IP Addresses for some host name.

Here is Example for this.
In this example we can get IP address of our given host name. We provide "jayeshsorathia.blogspot.com" as a host name. It will return Multiple IP Addresses.
We will also attach another output screen in which we supplied "www.microsoft.com" as a hostname.

C# Examples :
        string strHostName = "jayeshsorathia.blogspot.com";
        //string strHostName = "www.microsoft.com";
        // Get DNS entry of specified host name
        IPAddress[] addresses = Dns.GetHostEntry(strHostName).AddressList;

        // The DNS entry may contains more than one IP addresses.
        // Iterate them and display each along with the type of address (AddressFamily).
        foreach (IPAddress address in addresses)
        {
            Response.Write(string.Format("{0} = {1} ({2})", strHostName, address, address.AddressFamily));
            Response.Write("<br/><br/>");
        }

VB.net Examples :
        Dim strHostName As String = "jayeshsorathia.blogspot.com"
        'string strHostName = "www.microsoft.com";
        ' Get DNS entry of specified host name
        Dim addresses As IPAddress() = Dns.GetHostEntry(strHostName).AddressList

        ' The DNS entry may contains more than one IP addresses.
        ' Iterate them and display each along with the type of address (AddressFamily).
        For Each address As IPAddress In addresses
            Response.Write(String.Format("{0} = {1} ({2})", strHostName, address, address.AddressFamily))
            Response.Write("<br/><br/>")
        Next

Output (jayeshsorathia.blogspot.com)

Output (www.microsoft.com) :

This type of C# Tips is very useful in day to day programming life.

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.




3 comments:

  1. Convert your VB programs to C# with over 99% accuracy with the VBConversions VB.Net to C# Converter, the most accurate VB.Net to C# Converter available. Free download available at www.vbconversions.com

    vb.net to c#

    ReplyDelete