1

I am a Java beginner, who is not familiar with Java socket programming. I need to handle the event of client side disconnection. I searched how to detect a socket disconnection with read*, write* function in Java, some one said:

read() will return -1 
readLine() returns null
readXXX() for any other X throws EOFException.
The only really reliable way to detect a lost TCP connection is to write to it. Eventually this will throw an IOException: connection   reset, but it takes at least two writes due to buffering.

I am not quite sure what does EOFException mean and what's difference between EOFException and IOException. The official manual said EOFException "Signals that an end of file or end of stream has been reachedunexpectedly during input. ", but I still don't understand what it is in TCP socket connection. Does it mean a tcp connection called close() function? What reason will cause IOException for a tcp connection? Thank you very much.

xiao su
  • 157
  • 1
  • 1
  • 16

1 Answers1

1

EOF Exception is a special case of IO Exception. (it is a subclass of IOException). EOF means 'End of File' and EOF exception means that End of File (or any stream in your case) has been reached unexpectedly. This is only a case which can cause IOException.

In your case your read fail means, in the connection is down. So, since there is a problem in connection, the stream has reached the end. So, check if there is any problem with the network connection or the socket at the other end.

These answer too may help you. https://stackoverflow.com/a/10724508/4848659

See the docs for whole set of subclasses of IOException as ChangedCharSetException, CharacterCodingException, CharConversionException, ClosedChannelException, EOFException, FileLockInterruptionException, FileNotFoundException, FilerException, FileSystemException, HttpRetryException, IIOException, InterruptedByTimeoutException,....

Gimhani
  • 1,318
  • 13
  • 23