简体   繁体   中英

Empty the InputStream (of a Socket)

Some facts:
- My application is the client.
- I have a Socket pool.
- Multiple Thread s 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM