GetHostEntry Method (String)
Collapse the table of content
Expand the table of content

Dns.GetHostEntry Method (String)


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

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

Public Shared Function GetHostEntry (
	hostNameOrAddress As String
) As IPHostEntry


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

The hostNameOrAddress parameter is null.


The length of hostNameOrAddress parameter is greater than 255 characters.


An error was encountered when resolving the hostNameOrAddress parameter.


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.

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.

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.


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

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

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
    Next index
End Sub    


for accessing DNS information. Associated enumeration: PermissionState.Unrestricted

.NET Framework
Available since 2.0
Return to top
© 2015 Microsoft