Create SSLSocket using SSLSocketFactory with set connection timeout

My code is here:

SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, getAllCerts(), new SecureRandom()); SSLSocketFactory factory = sslContext.getSocketFactory(); mSocket = (SSLSocket) factory.createSocket("myhost.com", socketPort[index]); 

I need to check the ports table and select the open one. Everything works fine, but on createSocket () my application frees up a lot of time. If I have 5 ports, and the latter is an open connection, it takes about 3 minutes.

How to set a timeout on SSLSocketFactory to speed up the connection?

+6
source share
2 answers

If you're still interested, you can use the idea provided at https://groups.google.com/forum/#!topic/android-developers/EYtMO5WoXXI

 import javax.net.ssl.SSLSocketFactory; // Create a socket without connecting SSLSocketFactory socketFactory = SSLSocketFactory.getDefault(); Socket socket = socketFactory.createSocket(); // Connect, with an explicit timeout value socket.connect(new InetSocketAddress(endpoint.mServer, endpoint.mPort), CONNECT_TIMEOUT); 
+6
source

Try wrapping an existing socket instead of:

 Socket socketConn = new Socket(); socketConn.connect(addr, DEFAULT_CONNECT_TIMEOUT); mSocket = socketConn.getSocketFactory().createSocket(socketConn, hostname, port, true); 
+2
source

Source: https://habr.com/ru/post/896295/


All Articles