开发者

java - how to find if the machine has Ipv6 interface?

开发者 https://www.devze.com 2023-01-22 16:01 出处:网络
Is it possible to determine if the machin开发者_开发技巧e has IPV6 enabled using any Java api?Look at the NetworkInterface class.It has methods for getting all the machine\'s network interfaces includ

Is it possible to determine if the machin开发者_开发技巧e has IPV6 enabled using any Java api?


Look at the NetworkInterface class. It has methods for getting all the machine's network interfaces including virtual ones. You could look at the InetAddress for each interface and use inetAddress.getAddress().length to check the number of bytes in the IP to distinguish between IPv4 and IPv6.


InetAddress a = InetAddress.getByName(“www.sun.com”);
if (a instanceof Inet6Address) {
    Inet6Address a2 = (Inet6Address) a;
    if (a2.isIPv4CompatibleAddress()) {
        ...
    } if (
        a2.isLinkLocalAddress()) {
        ...
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消