Click here to Skip to main content
15,881,600 members
Articles / Programming Languages / C#
Article

Get IP Address from Web host name

Rate me:
Please Sign up or sign in to vote.
2.69/5 (8 votes)
10 Apr 2002 96.5K   33   7
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

C#
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Vietnam Vietnam
Nguyen Ha Giang,BS(computer science).

Comments and Discussions

 
Generalping -a to get the hostname from an ipaddress. Pin
vij_guy11-Jun-08 9:34
vij_guy11-Jun-08 9:34 
QuestionCan I help you! Pin
nguyenquanglamcnttk2718-Apr-07 7:18
nguyenquanglamcnttk2718-Apr-07 7:18 
GeneralGet host name from IP address Pin
Abdulghani Sabbagh18-Apr-06 21:36
Abdulghani Sabbagh18-Apr-06 21:36 
GeneralThanks a lot Pin
mansir 12330-Mar-06 19:50
mansir 12330-Mar-06 19:50 
GeneralThank you Nguyen! Pin
David Roh17-Aug-05 7:31
David Roh17-Aug-05 7:31 
This save me a lot of time looking through the docs - Thanks for taking the time to do it.!

David Roh
GeneralCool Pin
Daniel Liedke20-May-05 2:16
professionalDaniel Liedke20-May-05 2:16 
GeneralPlease don't post something like this! Pin
Anonymous17-Mar-05 11:14
Anonymous17-Mar-05 11:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.