Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ConnectionTimeoutException and SSLException with message "Connection timed out"

On my Android code (using Apache HTTP Client) I have already set the following parameters :

HttpConnectionParams.setConnectionTimeout(params, 30 * SECOND_IN_MILLIS);
HttpConnectionParams.setSoTimeout(params, 30 * SECOND_IN_MILLIS);

I am connecting to an HTTPS web-service. On slow networks, I get a ConnectionTimeoutException after expected 30 seconds (and I retry then) ; but there are "few" cases (with no pattern I could observe), I get either of these :

javax.net.ssl.SSLException: Read error: ssl=0xe71160: I/O error during system call, Connection timed out javax.net.ssl.SSLException: Write error: ssl=0xe71160: I/O error during system call, Broken pipe

I got this code from SO, which is still under test :

registry.register(new Scheme("https", 
    SSLCertificateSocketFactory.getHttpSocketFactory(30 * SECOND_IN_MILLIS, null), 443));

I can understand Connection establishment timeout and socket timeout - my query is do we really need this 3rd timeout for SSL ? How is the purpose different from SO_TIMEOUT ?

PS : These excepions seem to come in picture while reading/writing data - which should have resulted in data loss - but I cannot observe that either.

like image 753
WinOrWin Avatar asked Nov 01 '12 12:11

WinOrWin


People also ask

What is the difference between read timeout and connection timeout?

The connection timeout is the timeout in making the initial connection; i.e. completing the TCP connection handshake. The read timeout is the timeout on waiting to read data1.

What is connection timeout?

Connection timeout is on the client's side, usually meaning that the client lost connection, or is unable to establish connection to a server for whatever reason (such as remote firewall is dropping the traffic or the server went down).

What is connection timeout exception?

SocketTimeoutException: Connection timed out) means that it takes too long to get respond from other device and your request expires before getting response.

How do I fix socket timeout exception?

If either the accept() or read() method, blocks for more than 5 seconds, a SocketTimeoutException is thrown, designating that a timeout has occurred. It is important to note that after this exception is thrown. the socket remains valid, so you can retry the blocking call or do whatever you want with the valid socket.


1 Answers

The difference is that the ConnectionTimeoutException occurs when no connection can be established at all. That means the server is not reachable at all.

The SSLException occurs when the connection was established already but during the SSL handshake the connection times out. Because the timeout occurs after the SSL handshake has started the SSLException is thrown as higher level exception.

You can try to check if a ConnectionTimeoutException is entered as the cause of the SSLException.

like image 142
Uwe Plonus Avatar answered Sep 20 '22 15:09

Uwe Plonus