0

In a web application using the System.Net.GetHostByAddress method, we're having issues with the method returning the wrong host name.

The problem is that given an IP address, it is consistently returning an incorrect host name, for Windows 7 clients only. Systems running Windows XP return their host name normally.

The code we use looks like this:

host = System.Net.Dns.GetHostByAddress(GetIPv4Address)
strComputerName = host.HostName

GetIPv4Address is the address accessing the web app. However, we've also tried directly hard coding IP addresses with the same results:

host = System.Net.Dns.GetHostByAddress("192.168.1.1")
strComputerName = host.HostName

The final wrinkle is that for any given IP address GetHostByAddress returns the same (incorrect) host name most of the time. However, every once in a while it will throw a SocketException: No such host is known.

Updates:

  • We switched to GetHostByAddress from GetHostEntry on the advice of comments on the API documentation.

  • GetHostEntry also returns incorrect results.

  • Further investigation has revealed that nslookup returns results similar to what we're getting from GetHost*. So it may not be a .NET issue. We're contacting our network admins to see if they can work it out.

4

1 に答える 1

0

GetHostByAddress is deprecated. Here's how I'm doing it:

string ComputerName;
try
{
    ComputerName = System.Net.Dns.GetHostEntry(IPAddress).HostName;
}
catch (Exception e)
{
    ComputerName = "Unknown";
}

This works for Windows 7 clients for me. I haven't tried GetHostByAddress, so ymmv.

于 2012-04-04T23:58:08.097 に答える