Export (0) Print
Expand All

Dns.GetHostByAddress Method

Gets DNS host information for an IP address.

Overload List

Creates an IPHostEntry instance from the specified IPAddress.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function GetHostByAddress(IPAddress) As IPHostEntry
[C#] public static IPHostEntry GetHostByAddress(IPAddress);
[C++] public: static IPHostEntry* GetHostByAddress(IPAddress*);
[JScript] public static function GetHostByAddress(IPAddress) : IPHostEntry;

Creates an IPHostEntry instance from an IP address.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function GetHostByAddress(String) As IPHostEntry
[C#] public static IPHostEntry GetHostByAddress(string);
[C++] public: static IPHostEntry* GetHostByAddress(String*);
[JScript] public static function GetHostByAddress(String) : IPHostEntry;

Example

[Visual Basic, C#, C++] The following example creates a IPHostEntry from an IPAddress.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of GetHostByAddress. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Public Sub DisplayHostAddress(IpAddressString As [String])
    Try
        Dim hostIPAddress As IPAddress = IPAddress.Parse(IpAddressString)
        
        ' Call the GetHostByAddress(IPAddress) method, passing an IPAddress object as an argument 
        ' to obtain an IPHostEntry instance, containing address information for the specified host.
        
        Dim hostInfo As IPHostEntry = Dns.GetHostByAddress(hostIPAddress)
        ' Get the IP address list that resolves to the host names contained in 
        ' the Alias property.
        Dim address As IPAddress() = hostInfo.AddressList
        ' Get the alias names of the above addresses in the IP address list.
        Dim [alias] As [String]() = hostInfo.Aliases
        
        Console.WriteLine(("Host name : " + hostInfo.HostName))
        Console.WriteLine(ControlChars.Cr + "Aliases :")
        Dim index As Integer
        For index = 0 To [alias].Length - 1
            Console.WriteLine([alias](index))
        Next index
        Console.WriteLine(ControlChars.Cr + "IP address list : ")

        For index = 0 To address.Length - 1
            Console.WriteLine(address(index))
        Next index
        
    Catch e As SocketException
        Console.WriteLine("SocketException caught!!!")
        Console.WriteLine(("Source : " + e.Source))
        Console.WriteLine(("Message : " + e.Message))
        
    Catch e As FormatException
        Console.WriteLine("FormatException caught!!!")
        Console.WriteLine(("Source : " + e.Source))
        Console.WriteLine(("Message : " + e.Message))
        
    Catch e As ArgumentNullException
        Console.WriteLine("ArgumentNullException caught!!!")
        Console.WriteLine(("Source : " + e.Source))
        Console.WriteLine(("Message : " + e.Message))
        
    Catch e As Exception
        Console.WriteLine("Exception caught!!!")
        Console.WriteLine(("Source : " + e.Source))
        Console.WriteLine(("Message : " + e.Message))
    End Try
End Sub 'DisplayHostAddress

[C#] 
try 
{
   IPAddress hostIPAddress = IPAddress.Parse(IpAddressString);
   IPHostEntry hostInfo = Dns.GetHostByAddress(hostIPAddress);
   // Get the IP address list that resolves to the host names contained in 
   // the Alias property.
   IPAddress[] address = hostInfo.AddressList;
   // Get the alias names of the addresses in the IP address list.
   String[] alias = hostInfo.Aliases;

   Console.WriteLine("Host name : " + hostInfo.HostName);
   Console.WriteLine("\nAliases :");
   for(int index=0; index < alias.Length; index++) {
     Console.WriteLine(alias[index]);
   } 
   Console.WriteLine("\nIP address list : ");
   for(int index=0; index < address.Length; index++) {
      Console.WriteLine(address[index]);
   }
}
catch(SocketException e) 
{
     Console.WriteLine("SocketException caught!!!");
   Console.WriteLine("Source : " + e.Source);
   Console.WriteLine("Message : " + e.Message);
}
catch(FormatException e)
{
     Console.WriteLine("FormatException caught!!!");
   Console.WriteLine("Source : " + e.Source);
   Console.WriteLine("Message : " + e.Message);
}
catch(ArgumentNullException e)
{
     Console.WriteLine("ArgumentNullException caught!!!");
   Console.WriteLine("Source : " + e.Source);
   Console.WriteLine("Message : " + e.Message);
}
catch(Exception e)
{
    Console.WriteLine("Exception caught!!!");
    Console.WriteLine("Source : " + e.Source);
    Console.WriteLine("Message : " + e.Message);
}

[C++] 
try {
   IPAddress* hostIPAddress = IPAddress::Parse(IpAddressString);
   IPHostEntry* hostInfo = Dns::GetHostByAddress(hostIPAddress);
   // Get the IP address list that resolves to the host names contained in
   // the Alias property.
   IPAddress* address[] = hostInfo->AddressList;
   // Get the alias names of the addresses in the IP address list.
   String*  alias[] = hostInfo->Aliases;

   Console::WriteLine(S"Host name : {0}", hostInfo->HostName);
   Console::WriteLine(S"\nAliases :");
   for (int index=0; index < alias->Length; index++)
      Console::WriteLine(alias->Item[index]);

   Console::WriteLine(S"\nIP address list : ");
   for (int index=0; index < address->Length; index++)
      Console::WriteLine(address->Item[index]);

} catch (SocketException* e) {
   Console::WriteLine(S"SocketException caught!!!");
   Console::WriteLine(S"Source : {0}", e->Source);
   Console::WriteLine(S"Message : {0}", e->Message);
} catch (FormatException* e) {
   Console::WriteLine(S"FormatException caught!!!");
   Console::WriteLine(S"Source : {0}", e->Source);
   Console::WriteLine(S"Message : {0}", e->Message);
} catch (ArgumentNullException* e) {
   Console::WriteLine(S"ArgumentNullException caught!!!");
   Console::WriteLine(S"Source : {0}", e->Source);
   Console::WriteLine(S"Message : {0}", e->Message);
} catch (Exception* e) {
   Console::WriteLine(S"Exception caught!!!");
   Console::WriteLine(S"Source : {0}", e->Source);
   Console::WriteLine(S"Message : {0}", e->Message);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Dns Class | Dns Members | System.Net Namespace

Show:
© 2015 Microsoft