Click here to Skip to main content
971 members (375 online)
Click here to Skip to main content
Add your own
alternative version

Stats

2.9K views
2 bookmarked
Posted 12 Apr 2012

Get IP Address from Web host name

, 12 Apr 2012
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

Share

About the Author

nhgiang
Web Developer
Vietnam Vietnam
This member doesn't quite have enough reputation to be able to display their biography and homepage.

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
RA-Web1 | 2.8.171207.1 | Last Updated 12 Apr 2012
Article Copyright 2012 by nhgiang
Everything else Copyright © RootAdmin, 1999-2017
Layout: fixed | fluid