在 Java 中返回 IPv6

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11974232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:07:56  来源:igfitidea点击:

Return IPv6 in Java

javaipv6

提问by Nick

Is there a way in Java to tell it to return IPv6 only? I've tried everything and can't get it to work.

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");
    }

The line I use to try to return IPv6 only:

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

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

This keeps returning IPv4. Anyone have any idea of why its doing this?

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

回答by Pr0gr4mm3r

java.net.Inet6Addressdoes 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.

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

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

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

InetAddress.getByName()-Documentation

InetAddress.getByName()-文档

Determines the IP address of a host, given the host's name. The host name can either be a machine name, such as "java.sun.com", or a textual representation of its IP address. If a literal IP address is supplied, only the validity of the address format is checked.

> For host specified in literal IPv6 address, either the form defined in RFC 2732 or the literal IPv6 address format defined in RFC 2373 is accepted.<

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

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

So if you want to get an IPv6-Address you need to define it within your parameter, or configure a DNS-Server to return the IPv6-Address instead of the IPv4-Address.

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

Another way to retrieve the IPv6-Address is using InetAddress.getAllByName("www.google.at")which returns all known IP-Addresses of the host.

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

For example you can use this method to filter the returned array, which return the first IPv6-Address or nullif the host don't have one:

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

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

UPDATE:For more functions, especially those affecting DNS-Servers, I recommend using the external library DNSJava, because the plain Java implementation of DNS support is poor.
http://www.dnsjava.org/

更新:对于更多功能,尤其是影响 DNS 服务器的功能,我建议使用外部库 DNSJava,因为 DNS 支持的纯 Java 实现很差。
http://www.dnsjava.org/

Current Code:

当前代码:

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();
  }

}

回答by lanwen

You could try to define JVM_ARGS

您可以尝试定义 JVM_ARGS

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

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

with that props it will prefer IPv6 addr on InetAddress#getByName

有了这个道具,它会更喜欢 IPv6 地址 InetAddress#getByName

More info: https://docs.oracle.com/javase/8/docs/technotes/guides/net/ipv6_guide/

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