安卓面试

Android日常问题收集

2020-01-14  本文已影响0人  飞指

如何在IPv4的网络中访问IPv6具体实现方案

  /**
     *
     * @param hostname
     * @return
     * @throws UnknownHostException
     */
    @Override
    public List<InetAddress> lookup(String hostname) throws UnknownHostException {
        if (hostname == null) {
            throw new UnknownHostException("hostname == null");
        } else {
            List<InetAddress> inetAddresses = new ArrayList<>();
            InetAddress[] host = InetAddress.getAllByName(hostname);
            for (InetAddress inetAddress : host
            ) {
                if (inetAddress instanceof Inet4Address) {
                    inetAddresses.add(0, inetAddress);
                } else {
                    inetAddresses.add(inetAddress);
                }
            }
            return inetAddresses;
        }
    }
上一篇 下一篇

猜你喜欢

热点阅读