在Java中返回IPv6

Java中有没有办法告诉它只返回IPv6? 我已经尝试过所有的东西而无法让它发挥作用。

try { InetAddress inet = InetAddress.getByName(hostName); boolean status = inet.isReachable(5000); if (status) { System.out.println(inet.getCanonicalHostName() + " Host Reached\t" + java.net.Inet6Address.getByName(hostName).getHostAddress()); } else { System.out.println(inet.getCanonicalHostName() + " Host Unreachable"); } } catch (UnknownHostException e) { System.err.println("Host does not exists"); } catch (IOException e) { System.err.println("Error in reaching the Host"); } 

我用来尝试仅返回IPv6的行:

 System.out.println(inet.getCanonicalHostName() + " Host Reached\t" + java.net.Inet6Address.getByName(hostName).getHostAddress()); 

这不断返回IPv4。 任何人都知道为什么这样做?

java.net.Inet6Address不会覆盖getByName()
因此它将始终返回特定的IPv4地址,除非您的参数本身采用有效的IPv6地址的forms,在这种情况下,此方法将返回Inet6Address-Object。

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

InetAddress.getByName() -文档

根据主机名称确定主机的IP地址。 主机名可以是计算机名称,例如“java.sun.com”,也可以是其IP地址的文本表示forms。 如果提供了文字IP地址,则仅检查地址格式的有效性。

>对于在文字IPv6地址中指定的主机,接受RFC 2732中定义的格式或RFC 2373中定义的文字IPv6地址格式。<

因此,如果要获取IPv6地址,则需要在参数中定义它,或者配置DNS服务器以返回IPv6地址而不是IPv4地址。

检索IPv6地址的另一种方法是使用InetAddress.getAllByName("www.google.at") ,它返回主机的所有已知IP地址。

例如,您可以使用此方法过滤返回的数组,该数组返回第一个IPv6地址,如果主机没有,则返回null

 public Inet6Address getIPv6Addresses(InetAddress[] addresses) { for (InetAddress addr : addresses) { if (addr instanceof Inet6Address) { return (Inet6Address) addr; } } return null; } 

更新:对于更多function,特别是那些影响DNS服务器的function,我建议使用外部库DNSJava,因为DNS支持的普通Java实现很差。
http://www.dnsjava.org/

现行代码:

 public class Ping { public void pingHost (String hostName) { try { InetAddress[] inet = InetAddress.getAllByName(hostName); String address = this.getIPv4Addresses(inet).getHostAddress(); boolean status = this.getIPv6Addresses(inet).isReachable(5000); if (status) { System.out.println(reverseDns(address) + " Host Reached\t" + this.getIPv6Addresses(inet).getHostAddress()); } else { System.out.println(this.getIPv6Addresses(inet).getCanonicalHostName() + " Host Unreachable"); } } catch (UnknownHostException e) { System.err.println("Host does not exists"); } catch (IOException e) { System.err.println("Error in reaching the Host"); } } public Inet6Address getIPv6Addresses(InetAddress[] addresses) { for (InetAddress addr : addresses) { if (addr instanceof Inet6Address) { return (Inet6Address) addr; } } return null; } public Inet4Address getIPv4Addresses(InetAddress[] addresses) { for (InetAddress addr : addresses) { if (addr instanceof Inet4Address) { return (Inet4Address) addr; } } return null; } public static String reverseDns(String hostIp) throws IOException { Resolver res = new ExtendedResolver(); Name name = ReverseMap.fromAddress(hostIp); int type = Type.PTR; int dclass = DClass.IN; Record rec = Record.newRecord(name, type, dclass); Message query = Message.newQuery(rec); Message response = res.send(query); Record[] answers = response.getSectionArray(Section.ANSWER); if (answers.length == 0) return hostIp; else return answers[0].rdataToString(); } } 

您可以尝试定义JVM_ARGS

-Djava.net.preferIPv4Stack=false -Djava.net.preferIPv6Addresses=true

有了这些道具,它会更喜欢InetAddress#getByName上的IPv6 addr

更多信息: https : //docs.oracle.com/javase/8/docs/technotes/guides/net/ipv6_guide/