This topic has not yet been rated Rate this topic

Dns.GetHostEntry Method (String)

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

Namespace:  System.Net
Assembly:  System (in System.dll)
public static IPHostEntry GetHostEntry(
	string hostNameOrAddress
)

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.
Exception Condition
ArgumentNullException

The hostNameOrAddress parameter is Nothing.

ArgumentOutOfRangeException

The length of hostNameOrAddress parameter is greater than 255 characters.

SocketException

An error was encountered when resolving the hostNameOrAddress parameter.

ArgumentException

The hostNameOrAddress parameter is an invalid IP address.

The GetHostEntry method queries a DNS server for the IP address that is associated with a host name or IP address. If the DNS query fails, the system may fall back to a NetBIOS name query depending on the configuration of the local computer.

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 name 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 databases 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.

IPv6 addresses are filtered from the results of the GetHostEntry method if the local computer does not have IPv6 installed. As a result, it is possible to get back an empty IPHostEntry instance if only IPv6 results where available for the hostNameOrAddress.parameter.

The Aliases property of the IPHostEntry instance returned is not populated by this method and will always be empty.

Note Note

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

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


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);
    }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
  • DnsPermission  

    for accessing DNS information. Associated enumeration: PermissionState.Unrestricted

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Exception
This method should not throw an exception when a host address cannot be found. If we want to do a lot of DNS queries  (I am using it to call project honeypot API) throwing exceptions is a big performance hit..
Forward DNS lookup issue has been fixed in .NET 4.0
In the comment above, 3vi1 mentions that this function fails if there isn't a forward host/A entry. I asked Microsoft about this and they explained that this issue has been fixed in .NET 4.0 and it does appear to be fixed.
IPv6
IPv6 caused me an exception on Windows 7 when trying to convert the IP address of the local PC. This is how I solved it:
unsigned int own_ip = 0;
IPHostEntry^ hostEntry = nullptr;
hostEntry = Dns::GetHostEntry("");
for (int i = 0; i < hostEntry->AddressList->Length; i++) {
  if (hostEntry->AddressList[i]->AddressFamily == AddressFamily::InterNetwork) {
  // found IPv4 address
      IPAddress^ ipAddress = gcnew IPAddress(hostEntry->AddressList[i]->Address);
      own_ip = ipAddress->Address;
      break;
  }
}
GetHostEntry Sample, using PowerShell
<#
.SYNOPSIS
Demonstrates use of the GetHostEntry method of System.Net.DNS Class
.DESCRIPTION
This script is a an MSDN sample, using PowerShell
.NOTES
File Name : Get-HostEntry.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell V2 CTP3
.LINK
Sample posted to:
http://pshscripts.blogspot.com/2009/04/get-hostentryps1.html
Original MSDN sample at:
http://msdn.microsoft.com/en-us/library/system.net.dns.gethostentry.aspx
.EXAMPLE
PSH [C:\foo]: .\Get-HostEntry.ps1 Cookham8
Host Name : Cookham8.cookham.net
Alias :
Address : fe80::d8ed:afe2:2a97:a596%14
Address : fe80::3953:f67b:2f1c:1323%10
Address : 10.10.1.120
Address : 10.10.1.115
.PARAM HostName
The name of the host to search for - the default is "localhost"
#>

param (
[string] $HostName = "localhost"
)

###
# Start of Script
###

#Get Host details

$hostentrydetails = [System.Net.Dns]::GetHostEntry($hostname)

# Print details:
"Host Name : {0}" -f $hostentrydetails.HostName
foreach ($alias in $hostentrydetails.alises) {
"Alias : {0}" -f $alias
}
foreach ($addr in $hostentrydetails.addresslist) {
"Address : {0}" -f $Addr.ipaddresstostring
}

# End of script
Don't use this function.
Dear MS:  Sometimes you don't want to have a host/A entry for every reverse/PTR entry.  Perhaps you have uniquely named PTRs for every interface on your routers but the devices are always accessed via a simple name/loopback address and you don't want the clutter in your forward zones.

The 'obsolete' function (Dns.Resolve) works the way people expect this function to work (i.e. just populate the hostname if that's all that can be found).  It will also provide you with something useful in the event that the only thing DNS can do is give you a name via a WINS-R lookup.

Having the compiler throw a warning when using Resolve, and suggesting this function as a replacement is confusing and _will_ lead to broken code.  The compiler warning should be changed to *not* suggest this method - or at least warn that it's not going to function the same in the event of missing forward records.