Greelings
Greelings

Reputation: 5434

"SocketTimeoutException: failed to connect to" only from the Samsung S5 device

I have a very strange issue with my TCP communication app. I made it a long time ago and I never experienced any problem. It's a simple send & receive packets through a TCP socket.

But I don't know how, this week I ran it on a Samsung Galaxy S5, and I did not succeed in establishing the connection with the server.

serverSocket.connect(new InetSocketAddress(mServerIPV6.getHostAddress(), Network.COMMUNICATION_PORT), (int) (3 * DateUtils.SECOND_IN_MILLIS));

always returns :

java.net.SocketTimeoutException: failed to connect to /fe80::d6ae:5ff:fe43:c6e9%wlan0%23 (port 60001) after 3000ms

but the craziest thing is that it succeed in finding the IP address of the server through the network service discovery (UDP). So the problem does not come from the Wi-Fi network and it only happens with this device (Samsung S5). Very strange. Any idea?

Upvotes: 0

Views: 113

Answers (1)

Joshua Best
Joshua Best

Reputation: 246

If you're using older devices you may need to enable TLS version 1.2.

You can do this with an Okhttpclient builder.

 public static OkHttpClient.Builder enableTls12OnPreLollipop(OkHttpClient.Builder client) {
        if (Build.VERSION.SDK_INT >= 16 && Build.VERSION.SDK_INT < 22) {
            try {
                SSLContext sc = SSLContext.getInstance("TLSv1.2");
                sc.init(null, null, null);
                client.sslSocketFactory(new Tls12SocketFactory(sc.getSocketFactory()));

                ConnectionSpec cs = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
                        .tlsVersions(TlsVersion.TLS_1_2)
                        .build();

                List<ConnectionSpec> specs = new ArrayList<>();
                specs.add(cs);
                specs.add(ConnectionSpec.COMPATIBLE_TLS);
                specs.add(ConnectionSpec.CLEARTEXT);

                client.connectionSpecs(specs);
            } catch (Exception exc) {
                Log.e("OkHttpTLSCompat", "Error while setting TLS 1.2", exc);
            }
        }

        return client;
    }

Upvotes: 0

Related Questions