Different results between nslookup and Java's InetAddress getHostName

Vasilis picture Vasilis · Sep 3, 2012 · Viewed 7.2k times · Source

I'm trying to write a simple java program that will return the dns names of ip addresses which I do using the following code:

InetAddress host = InetAddress.getByName(ip);
String dnsName = host.getHostName();

When a dns name is registered getHostName() returns this name and when there is no existent dns name the ip address is returned.

For many addresses the above code doesn't return any while the nslookup command returns.

For example, for the address 82.117.193.169 nslookup returns peer-AS31042.sbb.rs while getHostName() returns only the address. This doesn't happen for all the addresses but for a large number of cases.

Answer

Peter Lawrey picture Peter Lawrey · Sep 3, 2012

Your computer may not be configured to use DNS by default, even if it is available on demand.

I would try

ping 82.117.193.169

and see whether it resolves the IP address into a hostname.