Create SSLSocket by SSLSocketFactory with set connection timeout

13,019

Solution 1

In case you are still wondering, you can use the idea given in 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);

Solution 2

Try wrapping an existing socket instead:

Socket socketConn = new Socket();
socketConn.connect(addr, DEFAULT_CONNECT_TIMEOUT);
mSocket = socketConn.getSocketFactory().createSocket(socketConn, hostname, port, true);
Share:
13,019
Chris
Author by

Chris

Updated on July 19, 2022

Comments

  • Chris
    Chris almost 2 years

    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 have to check table of ports and choose the open one. Everything works ok, but on createSocket() my application loose a lot of time. If I have 5 ports and the last is open connecting takes about 3 minutes.

    How can I set timeout on SSLSocketFactory to speed up connection?

  • class stacker
    class stacker over 11 years
    Did you test this? I would expect the createSocket() method in your last line to close the socket first, because it is already in a connected (non-SSL) state. Also, according to the docs, SSL Socket Factories with a specified SSL handshake timeout reset the underlying's socket timeout to 0 (aka no timeout) after the SSL protocol is finished establishing the connection.
  • user207421
    user207421 about 10 years
    @ClassStacker You are mistaken. I've done this. It works. (1) This is how you implement the STARTTLS command of SMTP, LSAP, etc, so you will find the same thing inside JavaMail, the JNDI LDAP provider, etc. (2) This is a connect timeout, not a read timeout, which is what your final sentence is about. (3) Please cite a reference for that claim. The word 'timeout' does not appear in the Javadoc for SSLSocketFactory or SSLSocket, except in the latter case as a reference to the methods inherited from Socket.
  • winklerrr
    winklerrr over 6 years
    This did the trick for me! Didn't think about the possibility to create a socket without a connection.
  • winklerrr
    winklerrr over 6 years
    Doesn't work in my case because I needed to ensure that the socket was bind to a certain network. With new Socket() the socket will still be bind to the default network.
  • user207421
    user207421 over 6 years
    @winklerrr That's not correct. The new socket isn't bound at all until you connect it, and it is then bound to the correct network for the connection, as determined by the IP routing to the target.
  • winklerrr
    winklerrr over 6 years
    @EJP you said to the "correct network by IP routing" - but that doesn't work in my case. How can I contact you?
  • user207421
    user207421 over 6 years
    @winklerrr You are contacting me. If you need to bind the socket, bind it, before connecting it.
  • winklerrr
    winklerrr over 6 years
    @EJP check my question here for more information about my problem. In my case it's not bound to the correct network.
  • Agent_L
    Agent_L over 5 years
    Doesn't work with SSL because java.net.SocketException: Unconnected sockets not implemented