Dns.GetHostAddresses Method

Definition

Overloads

GetHostAddresses(String)

Returns the Internet Protocol (IP) addresses for the specified host.

GetHostAddresses(String, AddressFamily)

Returns the Internet Protocol (IP) addresses for the specified host.

GetHostAddresses(String)

Returns the Internet Protocol (IP) addresses for the specified host.

public:
 static cli::array <System::Net::IPAddress ^> ^ GetHostAddresses(System::String ^ hostNameOrAddress);
public static System.Net.IPAddress[] GetHostAddresses (string hostNameOrAddress);
static member GetHostAddresses : string -> System.Net.IPAddress[]
Public Shared Function GetHostAddresses (hostNameOrAddress As String) As IPAddress()

Parameters

hostNameOrAddress
String

The host name or IP address to resolve.

Returns

An array of type IPAddress that holds the IP addresses for the host that is specified by the hostNameOrAddress parameter.

Exceptions

hostNameOrAddress is null.

The length of hostNameOrAddress is greater than 255 characters.

An error is encountered when resolving hostNameOrAddress.

hostNameOrAddress is an invalid IP address.

Examples

The following code example uses the GetHostAddresses method to resolve an IP address to an array of type IPAddress.

    // Determine the Internet Protocol(IP) addresses for a host.
public:
    static void DoGetHostAddress(String^ hostname)
    {
        array<IPAddress^>^ addresses;
        addresses = Dns::GetHostAddresses(hostname);

        Console::WriteLine("GetHostAddresses({0}) returns:", hostname);
        for each (IPAddress^ address in addresses)
        {
            Console::Write("{0} ", address);
        }
        Console::WriteLine("");
   }
public static void DoGetHostAddresses(string hostname)
{
    IPAddress[] addresses = Dns.GetHostAddresses(hostname);

    Console.WriteLine($"GetHostAddresses({hostname}) returns:");

    foreach (IPAddress address in addresses)
    {
        Console.WriteLine($"    {address}");
    }
}
Public Sub DoGetHostAddresses(hostName As String)

    Dim addresses As IPAddress() = Dns.GetHostAddresses(hostname)

    Console.WriteLine($"GetHostAddresses({hostname}) returns:")

    Dim index As Integer
    For index = 0 To addresses.Length - 1
        Console.WriteLine($"    {addresses(index)}")
    Next index

End Sub

Remarks

The GetHostAddresses method queries the DNS subsystem for the IP addresses associated with a host name. If hostNameOrAddress is an IP address, this address is returned without querying the DNS server.

If an empty string is passed as the hostNameOrAddress argument, then this method returns the IPv4 and IPv6 addresses of the local host.

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

This method is implemented using the underlying operating system's name resolution APIs (such as the Win32 API getaddrinfo on Windows, and equivalent APIs on other platforms). If a host is described in the hosts file, the IP address or addresses there will be returned without querying the DNS server.

Note

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

Applies to

GetHostAddresses(String, AddressFamily)

Returns the Internet Protocol (IP) addresses for the specified host.

public:
 static cli::array <System::Net::IPAddress ^> ^ GetHostAddresses(System::String ^ hostNameOrAddress, System::Net::Sockets::AddressFamily family);
public static System.Net.IPAddress[] GetHostAddresses (string hostNameOrAddress, System.Net.Sockets.AddressFamily family);
static member GetHostAddresses : string * System.Net.Sockets.AddressFamily -> System.Net.IPAddress[]
Public Shared Function GetHostAddresses (hostNameOrAddress As String, family As AddressFamily) As IPAddress()

Parameters

hostNameOrAddress
String

The host name or IP address to resolve.

family
AddressFamily

The address family for which IPs should be retrieved. If Unspecified, retrieve all IPs regardless of address family.

Returns

An array of type IPAddress that holds the IP addresses for the host that is specified by the hostNameOrAddress parameter.

Applies to