I have a SSLSocket pool and I need to check if the socket connection is sane before borrowing the object.
The isConnected, isInputShutdown and isOutputShutdown are useless because they don’t check if both sides are connected and if I try to write or read from the socket I might get a SocketException.
Here is my code for testing a connection:
if(sslSocket.isConnected() &&
!sslSocket.isInputShutdown() &&
!sslSocket.isOutputShutdown()){
valid = true;
}
This code doesn’t work, as explained above.
So, what is the best way to check if a SSLSocket (or a regular Socket) is sane?
If by “sane” you mean connected, you can only detect that a TCP socket is disconnected once you’ve tried to write to it:
http://lkml.indiana.edu/hypermail/linux/kernel/0106.1/1154.html
Hence, you need to make use of the exception accordingly in Java.