简体   繁体   English

Inet6Address.getByName()是否返回(现有)IPv6,但仅返回IPv4地址?

[英]Inet6Address.getByName() does return (existing) IPv6 but only IPv4 address?

The following always prints the first returned IPv4 address and not the first IPv6 address although I explicitly used Inet6Adress and not InetAddress. 尽管我明确使用了Inet6Adress而不是InetAddress,但以下内容始终打印返回的第一个IPv4地址,而不是第一个IPv6地址。 Is this a bug in the OpenJDK 1.7.0 I use? 这是我使用的OpenJDK 1.7.0中的错误吗?

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)? 如何为给定的主机名获取一个IPv6地址(如果不存在,则为NULL)?

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. 我不想在IPv4和IPv6之间更改解析器首选项,在这种情况下,我真的想查看是否存在至少一个IPv6地址。

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. 可以通过迭代InetAddress.getAllByName()的所有结果并检查是否有任何“ instanceof Inet6Address”来做到这一点,但这看起来并不像所设想的那样。

Just found this answer . 刚找到这个答案

I am copying textually the answer of the user Pr0gr4mm3r 我正在按文本复制用户Pr0gr4mm3r的答案

java.net.Inet6Address does not override getByName() java.net.Inet6Address不会覆盖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. 因此,除非您的参数本身采用有效的IPv6-Address形式,否则它将始终返回特定的IPv4-Address,在这种情况下,此方法将返回Inet6Address-Object。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM