-1

Recently we met SocketTimeoutException when the client application connecting to server. The client app utilized Axis1.4 as the SOAP engine. We already set the connection timeout and read timeout as 20 seconds for socket, but it looks like the connection timeout value doesn't work.

After digging into the socket creation in JDK, the socket factory used is sun.security.ssl.SSLSocketFactoryImpl, and the socket impl is sun.security.ssl.SSLSocketImpl. The problem is in the initialization of SSLSocketImpl, it hard-coded the connection timeout value as 0, that's mean there's no timeout:

SSLSocketImpl(SSLContextImpl context, InetAddress host, int port)
        throws IOException {
    super();
    init(context, false);
    SocketAddress socketAddress = new InetSocketAddress(host, port);
    connect(socketAddress, 0);
}

And the connection method is:

 /**
 * Connects this socket to the server with a specified timeout
 * value.
 *
 * This method is either called on an unconnected SSLSocketImpl by the
 * application, or it is called in the constructor of a regular
 * SSLSocketImpl. If we are layering on top on another socket, then
 * this method should not be called, because we assume that the
 * underlying socket is already connected by the time it is passed to
 * us.
 *
 * @param   endpoint the <code>SocketAddress</code>
 * @param   timeout  the timeout value to be used, 0 is no timeout
 * @throws  IOException if an error occurs during the connection
 * @throws  SocketTimeoutException if timeout expires before connecting
 */
public void connect(SocketAddress endpoint, int timeout)
        throws IOException {

    if (self != this) {
        throw new SocketException("Already connected");
    }

    if (!(endpoint instanceof InetSocketAddress)) {
        throw new SocketException(
                              "Cannot handle non-Inet socket addresses.");
    }

    super.connect(endpoint, timeout);
    doneConnect();
}

Why sun's implementation hard-coded 0? Is this a bug? From this to see the application need handle timeout by itself, this is weird.

zgcharley
  • 1,047
  • 3
  • 17
  • 24

1 Answers1

-2

We already set the connection timeout and read timeout as 20 seconds for socket

No you didn't. You called SSLSocketFactory.createSocket(host, port), which doesn't have a timeout parameter. So the default was used.

Is this a bug?

No. You didn't use a connect timeout, so it uses the default.

Why sun's implementation hard-coded 0?

Because that's the default.

user207421
  • 289,834
  • 37
  • 266
  • 440
  • Hi, see the constructor, during the initialization, it has "connect(socketAddress, 0)", zero is set here. If the application invoke the "public void connect(address, timeout)" explicitly, then for sure you can set pass the timeout value to the method. If the application create the socket by using socket factory, then the constructor is invoked, this time zero is always used. – zgcharley Feb 10 '16 at 04:42
  • Of course zero is used. That's the default. If you want a timed connect you have to create an unconnected socket and then call `connect()`. – user207421 Jul 05 '16 at 00:14