Android IPV6获取方式

2,795 阅读1分钟

下面的代码中我做了校验真实的ipv6获取ipv6去除%后面的值然后通过冒号校验位数,然后截取第一段判断是否包括fe或者fc包括就表示假的

 //ipv6
    public static String getLocalIpV6() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    // logger.error("ip1       " + inetAddress);
                    logger.error("ip1  " + inetAddress.getHostAddress());
                 /*   logger.error("getHostName  " + inetAddress.getHostName());
                    logger.error("getCanonicalHostName  " + inetAddress.getCanonicalHostName());
                    logger.error("getAddress  " + Arrays.toString(inetAddress.getAddress()));
                    logger.error("getHostAddress  " + inetAddress.getHostAddress());*/

                    if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet6Address) {
                        return inetAddress.getHostAddress();
                    }


                }
            }
        } catch (Exception ex) {
            Log.e("IP Address", ex.toString());
        }
        return null;
    }
    public static String getlocalIp() {
        String ip;

        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) {
//                           ip=inetAddress.getHostAddress().toString();
                        System.out.println("ip==========" + inetAddress.getHostAddress());
                        return inetAddress.getHostAddress();

                    }
                }
            }
        } catch (SocketException ignored) {

        }
        return null;
    }

    public static String validateV6() {
       
       new Thread(new Runnable() {
            @Override
            public void run() {
                hostIp6 = getLocalIpV6();
            }
        }).start();

        //过滤找到真实的ipv6地址
        logger.error("v6 validateV6 " + hostIp6);
        if (hostIp6 != null && hostIp6.contains("%")) {
            String[] split = hostIp6.split("%");
            String s1 = split[0];
            logger.error("v6 remove % is " + s1);

            if (s1 != null && s1.contains(":")) {
                String[] split1 = s1.split(":");
                if (split1.length == 6||split1.length==8) {
                    if (split1[0].contains("fe") || split1[0].contains("fc")) {
                        return "0.0.0.0";
                    } else {
                        return s1;
                    }
                }
            }
        }
        return "0.0.0.0";
    }