Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Dns.GetHostByName Method

Gets the DNS information for the specified DNS host name.

[Visual Basic]
Public Shared Function GetHostByName( _
   ByVal hostName As String _
) As IPHostEntry
[C#]
public static IPHostEntry GetHostByName(
 string hostName
);
[C++]
public: static IPHostEntry* GetHostByName(
 String* hostName
);
[JScript]
public static function GetHostByName(
   hostName : String
) : IPHostEntry;

Parameters

hostName
A string containing the DNS name of the host.

Return Value

An IPHostEntry object containing host information for the address specified in hostName.

Exceptions

Exception Type Condition
ArgumentNullException hostName is a null reference (Nothing in Visual Basic).
SocketException An error is encountered when resolving hostName.
SecurityException The caller does not have permission to access DNS information.

Remarks

The GetHostByName method queries the Internet DNS server for host information.

For asychronous access to DNS information, use the BeginGetHostByName and EndGetHostByName methods.

Example

[Visual Basic, C#, C++] The following example uses the GetHostByName to gets the DNS information for the specified DNS host name.

[Visual Basic] 
Public Sub DisplayHostName(hostName As [String])
    Try
        ' Call the GetHostByName method, passing a DNS style host name(for example,
        ' "www.contoso.com") as an argument to obtain an IPHostEntry instance, that 
        ' contains information for the specified host.
        
        Dim hostInfo As IPHostEntry = Dns.GetHostByName(hostName)
        ' 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 the 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 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

[C#] 
try 
{
   IPHostEntry hostInfo = Dns.GetHostByName(hostName);
   // 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(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 {
   IPHostEntry* hostInfo = Dns::GetHostByName(hostName);
   // 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 (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.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

.NET Framework Security: 

See Also

Dns Class | Dns Members | System.Net Namespace

Show:
© 2015 Microsoft