TCP套接字中的IOException和EOFException有什么区别



我是一个Java初学者,不熟悉Java套接字编程。我需要处理客户端断开连接的事件。我搜索了如何在 Java 中使用读*、写*功能检测套接字断开连接,有人说:

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.

我不太确定EOFException是什么意思,EOFException和IOException有什么区别。官方手册说EOFException"表示在输入过程中意外到达文件末尾或流结束",但我仍然不明白TCP套接字连接中是什么。这是否意味着名为close((函数的TCP连接?什么原因会导致 TCP 连接的 IOException? 谢谢。

EOF ExceptionIO Exception的特例。(它是IOException的一个子类(。EOF 表示"文件结束",EOF 异常表示意外到达文件结束(或您情况下的任何流(。这只是可能导致IOException的情况。

在您的情况下,读取失败意味着连接已关闭。因此,由于连接存在问题,因此流已到达终点。因此,请检查网络连接或另一端的套接字是否有任何问题。

这些答案也可能对您有所帮助。 https://stackoverflow.com/a/10724508/4848659

请参阅文档以获取IOException的整套子类作为ChangedCharSetException, CharacterCodingException, CharConversionException, ClosedChannelException, EOFException, FileLockInterruptionException, FileNotFoundException, FilerException, FileSystemException, HttpRetryException, IIOException, InterruptedByTimeoutException,....

最新更新