65.9K
CodeProject is changing. Read more.
Home

Get IP Address from Web host name

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.69/5 (8 votes)

Apr 11, 2002

viewsIcon

97291

Using network classes.

Sample Image - GTestDNS.jpg

Introduction

If our computer is on a LAN and has a DNS server, we can use the code below to get its IP address from the Web host. This code is very simple. We use the Dns class to connect to the DNS server on our Local Area Network. Then it returns a IPHostEntry object as IPHost. IPHost contains properties including the IP Address...

Implementation

using System;
using System.Net;
using System.Net.Sockets;
class GTest
{
    public static void Main()
    {
        string strHost;
        Console.Write("Input host : "); //Iput web host name as string
        strHost = Console.ReadLine();
        IPHostEntry IPHost = Dns.Resolve(strHost); // though Dns to get IP host
        Console.WriteLine(IPHost.HostName); // Output name of web host
        IPAddress [] address = IPHost.AddressList; // get list of IP address
        Console.WriteLine("List IP {0} :",IPHost.HostName); 
        for(int i = 0;i< address.Length; i++) // output list of IP address
            Console.WriteLine(address[i]);
    }
}

Reference

  • MSDN library.