InetAddress.getHostAddress() ipv6 兼容吗?
是 InetAddress.getHostAddress( ) JDK 1.6 中是否兼容 ipv6?
具体来说,我正在做
InetAddress.getLocalHost().getHostAddress()
ipv6 兼容吗?它适用于 ipv4 和 v6 地址吗?
Is InetAddress.getHostAddress() ipv6 compliant in JDK 1.6?
Specifically I am doing
InetAddress.getLocalHost().getHostAddress()
Is it ipv6 compliant? Does it work for both ipv4 and v6 addresses?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
扩展类
java.net.Inet6Address
符合 IPv6 标准。
Java文档:
基本上,如果您执行
InetAddress.getByName()
或InetAddress.getByAddress()
,这些方法会识别名称或地址是 IPv4 还是 IPv6 名称/地址,并返回扩展的名称/地址。分别为Inet4Address
/Inet6Address
。至于
InetAddress.getHostAddress()
,它返回一个null。您将需要 java.net.Inet6Address.getHostAddress() 来返回可表示的 IPv6 字符串地址。The extended class
java.net.Inet6Address
is IPv6 compliant.JavaDoc:
Basically, if you do
InetAddress.getByName()
orInetAddress.getByAddress()
the methods identify whether the name or address is an IPv4 or IPv6 name/address and return an extendedInet4Address
/Inet6Address
respectively.As for
InetAddress.getHostAddress()
, it returns a null. You will needjava.net.Inet6Address.getHostAddress()
to return an IPv6 string representable address.我查看了 InetAddress 类的代码,它确实做了正确的事情。
I looked at the code of InetAddress class and it is indeed doing the right thing.
根据以上分析,测试代码如下:
Here is the code to test based on the above analysis: