If I have a Selector
that's accepting sockets
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 thread
then takes one of those now closed sockets
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?
Asked
Active
Viewed 150 times
1

mal
- 3,022
- 5
- 32
- 62
-
3You can't read from a closed socket. – Arnaud Apr 28 '16 at 08:57
1 Answers
1
It depends on what 'severed' means.
- If you closed the socket yourself, you will get
SocketException: socket closed
from the variousread()
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.

user207421
- 305,947
- 44
- 307
- 483
-
OK thanks, I think the second one is what I'm seeing on my server. Can you clarify what "abortively reset" means? – mal Apr 29 '16 at 10:15
-
1There are various circumstances under which a connection is reset instead of closed nicely: process exits without closing (Windows); process closes socket while there is still unread data in the socket read buffer; host receives data for connection that is already closed at its end, ... All these cause a TCP RST to be issued. – user207421 May 03 '16 at 02:50