bot_bot bot_bot - 2 years ago 121
Java Question

Are bytes on a TCP socket connection lost if the connection is severed before the bytes are read?

If I have a

that's accepting
and pushing them to a queue for other threads to handle, and no threads takes a socket for, say two minutes, and the client pushes data but then times out and disconnects. When a
then takes one of those now closed
and tries to read from it, will it get the data that has already been sent by the client, or will there be nothing there to read?

Answer Source

It depends on what 'severed' means.

  • If you closed the socket yourself, you will get SocketException: socket closed from the various read() methods.
  • If the connection was closed normally by the peer, all the pending data will be read before end of stream is signalled.
  • If the connection was abortively reset, all the pending data is lost.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download