本文介绍了在Java中返回IPv6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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的行:

The line I use to try to return IPv6 only:

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

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

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

推荐答案

java.net.Inet6Address 不会覆盖 getByName()

因此它将始终返回特定的IPv4地址
,除非您的参数本身在有效的IPv6地址的形式,在这种情况下,此方法将返回Inet6Address-Object。

java.net.Inet6Address does 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.

例如:

getByName(stackoverflow.com) - > Inet4Address

getByName(2001:0db8:85a3:08d3:1319:8a2e:0370:7344) - > Inet6Address

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

InetAddress.getByName() - 文档

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

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

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地址的另一种方法是使用 InetAddress.getAllByName(www.google.at)返回所有已知的IP-主持人的地址。

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

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

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

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

更新:
更多功能,尤其是影响DNS服务器的那些,我建议使用外部库DNSJava,因为DNS支持的普通Java实现很差。

当前代码:

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

}

这篇关于在Java中返回IPv6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 15:57