Dns.Resolve() vs Dns.Gethostentry()

Asked

Viewed 722 times

1

The method Resolve is as obsolete and Microsoft guides to use the GetHostEntry. However when using GetHostEntry the exception occurs:

This host is not known

When I use Resolve there is no exception and the program works correctly.

The Ip’s used are from the LAN my pc is connected to. I did a test by placing a remote Ip, from Google, and then the GetHostEntry did not generate exception, IE, it does not work with the Ip’s of my lan.

How can I solve this problem using GetHostEntry?

1 answer

1


This happens because the method GetHostEntry tries to make a reverse search before returning the IP, if this procedure fails, the error is returned WSAHOST_NOT_FOUND - Host not found.

Alternatively, you can use the method GetHostAddresses, than different from the method GetHostEntry, no reverse search is made, the IP is immediately returned as a result.

Example:

public static void DoGetHostAddresses(string hostname)
{
    IPAddress[] ips;    
    ips = Dns.GetHostAddresses(hostname);

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

    foreach (IPAddress ip in ips)
    {
        Console.WriteLine("    {0}", ip);
    }
}

Source

Browser other questions tagged

You are not signed in. Login or sign up in order to post.