1

I am trying to convert ip addresses to hostnames. I tried the answer given here. While the linux command "host" works for all the ip addresess I have, this code only works for some of them. Why would this be?

  InetAddress addr = InetAddress.getByName("192.168.190.62");
  String host = addr.getHostName();
  System.out.println(host);
Community
  • 1
  • 1
Porcupious
  • 31
  • 3
  • Where is your code ? – Raptor Oct 08 '14 at 01:50
  • I've just added in the code I'm using. I basically took it from the other answer on stackoverflow. – Porcupious Oct 08 '14 at 01:56
  • The code works or not depend on your DNS. – Raptor Oct 08 '14 at 02:15
  • I'm on a corporate network. Is there a way/need for me to force the DNS server or such? I'm surprised that the command "host" and this work differently, I would have expected them to be identical. – Porcupious Oct 08 '14 at 02:25
  • I just noticed you use private IP. Do you want to resolve computer host name instead (then this is not related to DNS, sorry for confusion). See [this](http://stackoverflow.com/questions/1881546/inetaddress-getlocalhost-throws-unknownhostexception) – Raptor Oct 08 '14 at 02:34
  • I'm actually not sure which one I want. However, it seems to me like the computer hostname works, but DNS resolves do not. Maybe host is smart enough to do reverse DNS but InetAddress is not? (for intranet dns server maybe) – Porcupious Oct 08 '14 at 20:30

1 Answers1

0

when looking at http://download.java.net/jdk7/archive/b123/docs/api/index.html?java/net/InetAddress.html you find...

getHostName

Gets the host name for this IP address.

If this InetAddress was created with a host name, this host name will be remembered and returned; otherwise, a reverse name lookup will be performed and the result will be returned based on the system configured name lookup service. If a lookup of the name service is required, call getCanonicalHostName().

Martin Frank
  • 3,445
  • 1
  • 27
  • 47
  • I tried using that, and I got the same result. Atleast, there are ip addresses that neither getHostName() or getCanonicalHostName() are unable to return a valid value, but "host" does. – Porcupious Oct 08 '14 at 20:27
  • as mentioned in the doc, it says the result will be returned **based in the system configuration name lookup service** maybe you can edit this configuration (hosts.txt)? – Martin Frank Oct 09 '14 at 04:21