Some facts:
– My application is the client.
– I have a Socket pool.
– Multiple Threads use this pool.
– Each Thread can timeout.
– On timeout, the Socket is returned to the pool, even if no reply was read from the server.
The problem:
On timeout, chances are that the next Thread that uses this Socket will have to read some old message (which was sent by the server after the Socket was returned).
The question:
What’s the best way to “purge” the InputStream of a Socket before use?
If your I/O protocol did not complete normally, the best solution is to close offending I/O channel, remove it from the pool, open a new connection and add it to the pool.
For that to work, both Client and Server should be prepared for this eventuality by providing a method that confirms the other end liveness.