Click here to Skip to main content
Click here to Skip to main content

Get IP Address from Web host name

, 10 Apr 2002
Rate this:
Please Sign up or sign in to vote.
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.

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

About the Author

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

Comments and Discussions

 
Generalping -a to get the hostname from an ipaddress. Pinmembervij_guy11-Jun-08 9:34 
QuestionCan I help you! Pinmembernguyenquanglamcnttk2718-Apr-07 7:18 
GeneralGet host name from IP address PinmemberAbdulghani Sabbagh18-Apr-06 21:36 
GeneralThanks a lot Pinmembermansir 12330-Mar-06 19:50 
GeneralThank you Nguyen! PinsussDavid Roh17-Aug-05 7:31 
GeneralCool PinmemberDaniel Carvalho Liedke20-May-05 2:16 
GeneralPlease don't post something like this! PinsussAnonymous17-Mar-05 11:14 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 11 Apr 2002
Article Copyright 2002 by nhgiang
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid