4

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。任何人都知道它为什么这样做?

4

2 回答 2

2

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

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

InetAddress.getByName()-文档

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

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

因此,如果您想获得 IPv6-Address,您需要在参数中定义它,或者配置 DNS-Server 以返回 IPv6-Address 而不是 IPv4-Address。

检索 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;
}

更新: 对于更多功能,尤其是那些影响 DNS-Servers 的功能,我建议使用外部库 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();
  }

}
于 2012-08-15T18:14:10.360 回答
1

您可以尝试定义 JVM_ARGS

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

有了这些道具,它会更喜欢 IPv6 addrInetAddress#getByName

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

于 2016-02-10T10:27:18.087 回答