Best way to check if IPv6 is available

匆匆过客 提交于 2020-01-06 05:08:45

问题


What is the best way to check if IPv6 is available on the currient android phone?

My currient idea is to use NetworkInterface and to enumerate via NetworkInterface.getNetworkInterfaces() but this seems to be too complicated.

Is there a simpler way?


回答1:


I don't know of a simpler way than using NetworkInterface if you need to check all of the interfaces, but it shouldn't be that bad:

for(NetworkInterface netInt: NetworkInterface.getNetworkInterfaces()){
    for(InterfaceAddress address: netInt.getInterfaceAddresses()){
        if(address.getAddress() instanceof Inet6Address){
            // found IPv6 address
            // do any other validation of address you may need here
        }
    }
}

if you know the address you want to check you can skip using NetworkInterface and check the specific InetAddress by calling one of InetAddress's static getBy...() methods and check whether that is an instance of Inet6Address.




回答2:


boolean isIPV6 = false;
Enumeration<NetworkInterface> networkInterfaces =
    NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
    NetworkInterface ni = networkInterfaces.nextElement();
    for (InterfaceAddress addr : ni.getInterfaceAddresses()) {
        if (addr.getAddress() instanceof Inet6Address) {
            isIPV6 = true;
        }
    }
}


来源:https://stackoverflow.com/questions/10141638/best-way-to-check-if-ipv6-is-available

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!