首页 > 解决方案 > Android 在某些设备上连接到 IPv6 地址失败

问题描述

该代码尝试使用 IPv4 和 IPv6 进行连接。

在某些设备上连接到 IPv6 失败,而同一 Wifi-Router 上的其他设备没有问题。

AsyncTask.execute(new Runnable() {
    @Override
    public void run() {
        try {
            InetAddress [] adrall = InetAddress.getAllByName("www.google.com");
            for(InetAddress adr : adrall)
            {
                try {
                    Log.d("TEST", "Connecting to " + adr.getHostAddress());
                    new Socket().connect(new InetSocketAddress(adr, 80), 2000);
                    Log.d("TEST", "Success!");
                } catch (Exception e) {
                    Log.d("TEST", "Fail!");
                }
            }
        } catch (Exception e) {
        }
    }
});

例如,使用三星 Galaxy S9 的输出是:

Connecting to 2a00:1450:4001:808::2004
Fail!
Connecting to 172.217.16.132
Success!

有任何想法吗?

标签: androidsocketsipv6samsung-galaxy

解决方案


推荐阅读