.NET Framework Class Library
Dns..::.GetHostEntry Method (String)

Resolves a host name or IP address to an IPHostEntry instance.

Namespace:  System.Net
Assembly:  System (in System.dll)
Syntax

Visual Basic (Declaration)
Public Shared Function GetHostEntry ( _
    hostNameOrAddress As String _
) As IPHostEntry
Visual Basic (Usage)
Dim hostNameOrAddress As String
Dim returnValue As IPHostEntry

returnValue = Dns.GetHostEntry(hostNameOrAddress)
C#
public static IPHostEntry GetHostEntry(
    string hostNameOrAddress
)
Visual C++
public:
static IPHostEntry^ GetHostEntry(
    String^ hostNameOrAddress
)
JScript
public static function GetHostEntry(
    hostNameOrAddress : String
) : IPHostEntry

Parameters

hostNameOrAddress
Type: System..::.String
The host name or IP address to resolve.

Return Value

Type: System.Net..::.IPHostEntry
An IPHostEntry instance that contains address information about the host specified in hostNameOrAddress.
Exceptions

ExceptionCondition
ArgumentNullException

The hostNameOrAddress parameter is nullNothingnullptra null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

The length of hostNameOrAddress parameter is greater than 126 characters.

SocketException

An error was encountered when resolving the hostNameOrAddress parameter.

ArgumentException

The hostNameOrAddress parameter is an invalid IP address.

Remarks

The GetHostEntry method queries a DNS server for the IP address that is associated with a host name or IP address.

When an empty string is passed as the host name, this method returns the IPv4 addresses of the local host.

If the host name could not be found, the SocketException exception is returned with a value of 11001 (Windows Sockets error WSAHOST_NOT_FOUND). This exception can be returned if the DNS server does not respond. This exception can also be returned if the name is not an official host name or alias, or it cannot be found in the database(s) being queried.

The ArgumentException exception is also returned if the hostNameOrAddress parameter contains Any or IPv6Any.

The GetHostEntry method assumes that if an IP literal string is passed in the hostNameOrAddress parameter that the application wants an IPHostEntry instance returned with all of the properties set. These properties include the AddressList, Aliases, and HostName. As a result, the implementation of the GetHostEntry method exhibits the following behavior when an IP string literal is passed:

  1. The method tries to parse the address. If the hostNameOrAddress parameter contains a legal IP string literal, then the first phase succeeds.

  2. A reverse lookup using the IP address of the IP string literal is attempted to obtain the host name. This result is set as the HostName property.

  3. The host name from this reverse lookup is used again to obtain all the possible IP addresses associated with the name and set as the AddressList property.

For an IPv4 string literal, all three steps above may succeed. But it is possible for a stale DNS record for an IPv4 address that actually belongs to a different host to be returned. This may cause step #3 to fail and throw an exception (there is a DNS PTR record for the IPv4 address, but no DNS A record for the IPv4 address).

For IPv6, step #2 above may fail, since most IPv6 deployments do not register the reverse (PTR) record for an IPv6 address. So this method may return the string IPv6 literal as the fully-qualified domain (FQDN) host name in the HostName property.

The GetHostAddresses method has different behavior with respect to IP literals. If step #1 above succeeds (it successfully parses as an IP address), that address is immediately returned as the result. There is no attempt at a reverse lookup.

NoteNote:

This member emits trace information when you enable network tracing in your application. For more information, see Network Tracing.

Examples

The following example uses the GetHostEntry method to resolve an IP address to an IPHostEntry instance.

Visual Basic
Public Sub DoGetHostEntry(hostName As [String])

    DIM host as IPHostEntry = Dns.GetHostEntry(hostname)

    Console.WriteLine("GetHostEntry(" + hostname + ") returns: ")

    Dim ip As IPAddress() = host.AddressList

    Dim index As Integer
    For index = 0 To ip.Length - 1
         Console.WriteLine(ip(index))
    Next index
End Sub    
C#
public static void DoGetHostEntry(string hostname)
{
    IPHostEntry host;

    host = Dns.GetHostEntry(hostname);

    Console.WriteLine("GetHostEntry({0}) returns:", hostname);

    foreach (IPAddress ip in host.AddressList)
    {
        Console.WriteLine("    {0}", ip);
    }
}
Visual C++
public:
        static void DoGetHostEntry(String^ hostname)
        {
            IPHostEntry^ host = Dns::GetHostEntry(hostname);

            Console::WriteLine("GetHostEntry({0}) returns:", host->HostName);

            for (int i = 0; i < host->AddressList->Length; i++)
            {                
                Console::WriteLine("    {0}", host->AddressList[i]->ToString());            
            }
        }
CPP_OLD
// Determine the Internet Protocol(IP) addresses for my host.
static void DoGetLocalHostAddress()
{
    IPAddress * myHost[] = Dns::GetLocalHostAddresses();

    Console::Write("IPs for my host: ");

    IEnumerator* ips = myHost->GetEnumerator();
    while (ips->MoveNext())
    {
        Console::Write("{0} ", ips->Current);
    }
    Console::WriteLine("");        
}
.NET Framework Security

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0
See Also

Reference

Tags :


Community Content

salat
correction needed

line : "When an empty string is passed as the host name, this method returns the IPv4 addresses of the local host."
should be changed to : "When an empty string is passed as the host name, this method returns IPv4 and IPv6 addresses of the local host."

Tags :

Christopher King
Obtaining HostName does not always work.

host = Dns.GetHostEntry(ipAddress) often times throws the exception (No Such Host is Known).... However, if the deprecated method Dns.GetHostByAddress(ipAddress) is used for this same IP Address, it works.

For EX: assume IP Address is 74.84.194.59 (which is www.whitehouse.com). Dns.GetHostEntry will return the (No Such Host is known) but if you use Dns.GetHostByAddress it will return (194-059.baymountain.com).

Possibly these methods use differents Dns Servers (Databases) to resolve the IP Address, not really sure. If anyone knows the exact cause of this I would love to hear from you.

Also, in the cases where an invalid IpAddress is passed in to these methods, the call takes entirely too long to return. It appears internally an Application.DoEvents() is being called, but the actual return from these methods takes like 10 seconds. At minimum there should be a timeout param on methods that can take this long to return to the caller.

Actually, what I see in that case is that it returns the same invalid IP passed. I am using .NET 3.5/VS 2008...

Tags : contentbug

Page view tracker