在Java中返回IPv6

时间:2012-08-15 17:37:13

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

2 个答案:

答案 0 :(得分:2)

java.net.Inet6Address不会覆盖getByName()
所以它将始终返回特定的IPv4地址, 除非您的参数本身是有效的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地址   提供时,只检查地址格式的有效性。

     

<强>&GT;对于在文字IPv6地址中指定的主机,可以是在中定义的表单   RFC 2732或RFC 2373中定义的文字IPv6地址格式是   接受&LT;

因此,如果您想获取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;
}

<强>更新 对于更多功能,特别是那些影响DNS服务器的功能,我建议使用外部库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();
  }

}

答案 1 :(得分:1)

您可以尝试定义JVM_ARGS

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

使用该道具,它会更喜欢InetAddress#getByName

上的IPv6地址

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