1

The following always prints the first returned IPv4 address and not the first IPv6 address although I explicitly used Inet6Adress and not InetAddress. Is this a bug in the OpenJDK 1.7.0 I use?

InetAddress ipv6 = Inet6Address.getByName("www.google.com");

How am I supposed to get one IPv6 adresse for a given host name (or NULL if none exists)?

I do not want to change the resolver preference between IPv4 and IPv6, in this case I really want to see if at least one IPv6 address exists.

It's probably doable by iterating all results of InetAddress.getAllByName() and check for any "instanceof Inet6Address" but that does not look like the supposed way to do it.

lathspell
  • 3,040
  • 1
  • 30
  • 49
  • possible duplicate of [Return IPv6 in Java](http://stackoverflow.com/questions/11974232/return-ipv6-in-java) – Steve-o Feb 04 '14 at 18:48

1 Answers1

0

Just found this answer .

I am copying textually the answer of the user Pr0gr4mm3r

java.net.Inet6Address does not override getByName()
so it will always return the specific IPv4-Address, unless your parameter itself is in the form of an valid IPv6-Address, in this case this method will return an Inet6Address-Object.

For example:
getByName("stackoverflow.com") --> Inet4Address
getByName("2001:0db8:85a3:08d3:1319:8a2e:0370:7344") --> Inet6Address

Community
  • 1
  • 1
Diego
  • 916
  • 1
  • 13
  • 36
  • That answer is incorrect: Java API simply does not support IP family preference in DNS resolution. – Steve-o Feb 04 '14 at 18:49
  • `InetAddress.getByName("ipv6.google.com")` can return `Inet6Address` [http://rextester.com/PNY17654](Example code). – Steve-o Feb 04 '14 at 18:54
  • I am making reference to this answer: http://stackoverflow.com/questions/11974232/return-ipv6-in-java The user is giving reference that it work – Diego Feb 04 '14 at 22:46