Click here to Skip to main content
1,837 members
Articles / Security / .NET 1.0
Article

Get IP Address from Web host name

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
12 Apr 2012 10.3K   2  
Using network classes.

This article is a sponsored article. Articles such as these are intended to provide you with information on products and services that we consider useful and of value to developers

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
This member doesn't quite have enough reputation to be able to display their biography and homepage.

Comments and Discussions

 
-- There are no messages in this forum --