I'm trying to write a simple program using Java that, given an IP in either version 4 or 6 format, will return its FQDN. The following code works fine when given an ipv4 address, but will only return the given address when an ipv6 address is entered.
InetAddress inet;
try { inet = InetAddress.getByName(theIpAddress); }
catch(UnknownHostException e) { System.out.println("Unknown Host"); return; }
System.out.println(inet.getHostAddress(););
System.out.println(inet.getHostName(););
Whenever I enter an ipv6 getHostName() will only return the same ipv6, even when I know that the ipv6 will resolve to a FQDN. Also, if I enter an ipv6 host name, such as ipv6.google.com, in place of theIpAddress, the exception will occur.
I'm new to this stuff so would appreciate any assistance. Thanks.
An IP address is represented by 32-bit or 128-bit unsigned number. InetAddress can handle both IPv4 and IPv6 addresses.
The getByName() method of InetAddress class determines the IP address of a host from the given host's name. If the host name is null, then an InetAddress representing an address of the loopback interface is returned.
The problem was actually the version of Java I was running. Updating Java to 1.6.23, from 1.6.21, allowed ipv6s to resolve to their FQDN.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With