Eldon Hipolito
Eldon Hipolito

Reputation: 724

Placing timeout for SSLSocket handshake

Recently, I am having trouble with our SOAP calls which uses apache axis 1.4. I have set the timeout as 30s, however it sometimes reaches around ~1000s before throwing an exception. After exploring internal implementation of axis, I found out that it is due to the SSLSocket handshake.

See sample error below.

java.net.SocketException: Connection timed out (Read failed)
at java.net.SocketInputStream.$$YJP$$socketRead0(Native Method)
at java.net.SocketInputStream.socketRead0(SocketInputStream.java)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
at sun.security.ssl.InputRecord.read(InputRecord.java:503)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)
at org.apache.axis.components.net.JSSESocketFactory.create(JSSESocketFactory.java:186)

Is it possible to put a timeout also on the handshake? Or is there another way to forcibly cut the connection.

Also, see the handshake part on the code

 sslSocket = sslFactory.createSocket(tunnel, host, port, true);
            if (log.isDebugEnabled()) {
                log.debug(Messages.getMessage("setupTunnel00",
                          tcp.getProxyHost(),
                        "" + tunnelPort));
            }
        }

        ((SSLSocket) sslSocket).startHandshake();

Upvotes: 0

Views: 663

Answers (1)

zhh
zhh

Reputation: 2406

When you create the tunnel, call:

tunnel.connect(new InetSocketAddress(host, port), 30000);

Upvotes: 1

Related Questions