InetAddress.getHostAddress() ipv6 兼容吗?

发布于 2024-11-27 05:37:01 字数 310 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可遇━不可求 2024-12-04 05:37:01

扩展类 java.net.Inet6Address符合 IPv6 标准。

Java文档:

此类表示 Internet 协议版本 6 (IPv6) 地址。
由 RFC 2373 定义:IP 版本 6 寻址架构。

基本上,如果您执行 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:

This class represents an Internet Protocol version 6 (IPv6) address.
Defined by RFC 2373: IP Version 6 Addressing Architecture.

Basically, if you do InetAddress.getByName() or InetAddress.getByAddress() the methods identify whether the name or address is an IPv4 or IPv6 name/address and return an extended Inet4Address/Inet6Address respectively.

As for InetAddress.getHostAddress(), it returns a null. You will need java.net.Inet6Address.getHostAddress() to return an IPv6 string representable address.

扮仙女 2024-12-04 05:37:01

我查看了 InetAddress 类的代码,它确实做了正确的事情。

  if (isIPv6Supported()) { 
      o = InetAddress.loadImpl("Inet6AddressImpl"); 
  } 
  else { 
      o = InetAddress.loadImpl("Inet4AddressImpl"); } 
      return (InetAddressImpl)o; 
  }

I looked at the code of InetAddress class and it is indeed doing the right thing.

  if (isIPv6Supported()) { 
      o = InetAddress.loadImpl("Inet6AddressImpl"); 
  } 
  else { 
      o = InetAddress.loadImpl("Inet4AddressImpl"); } 
      return (InetAddressImpl)o; 
  }
懷念過去 2024-12-04 05:37:01

根据以上分析,测试代码如下:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    InetAddress localIP;
    try {
        localIP = InetAddress.getLocalHost();
         if(localIP instanceof Inet6Address){
             System.out.println("IPV6");
         } else if (localIP instanceof Inet4Address) {
             System.out.println("IPV4");
         }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

}

Here is the code to test based on the above analysis:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    InetAddress localIP;
    try {
        localIP = InetAddress.getLocalHost();
         if(localIP instanceof Inet6Address){
             System.out.println("IPV6");
         } else if (localIP instanceof Inet4Address) {
             System.out.println("IPV4");
         }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文