gpt4 book ai didi

java - 输入套接字意外关闭时吞下 IOException

转载 作者:行者123 更新时间:2023-11-30 10:52:10 26 4
gpt4 key购买 nike

当套接字意外关闭时,此代码段失败。不IOException 被抛出,并且 s.read() 永远不会返回。

  try {
while (!doneReading)
{ /* read blocks waiting for data */
int readResult = s.read(inBuf, inBufLength, 1);
...
}
}
catch (IOException err) { .. }

当socket初始关闭时,栈如下:

    Thread [Thread-10] (Suspended (exception SocketException))  
SocketInputStream.socketRead0(FileDescriptor, byte[], int, int, int) line: not available [native method]
SocketInputStream.read(byte[], int, int, int) line: 150
SocketInputStream.read(byte[], int, int) line: 121
SocketInputStream.read(byte[]) line: 107
JavaSEPort$SocketImpl.readFromStream() line: 7462
JavaSEPort.readFromSocketStream(Object) line: 7601
Socket$SocketInputStream.read(byte[], int, int) line: 179
NetConn.runRead() line: 1044
NetConn.run() line: 989
Thread.run() line: 744
C:\java\jdk1.8.0\bin\javaw.exe (Dec 23, 2015, 4:47:53 PM)

socketexception 显然被捕获,并作为 ioexception 重新抛出此时:

    Thread [Thread-10] (Suspended (exception IOException))  
owns: DualStackPlainSocketImpl (id=93)
DualStackPlainSocketImpl(AbstractPlainSocketImpl).available() line: 465 [local variables unavailable]
SocketInputStream.available() line: 238
JavaSEPort$SocketImpl.getAvailableInput() line: 7428
JavaSEPort$SocketImpl.readFromStream() line: 7452
JavaSEPort.readFromSocketStream(Object) line: 7601
Socket$SocketInputStream.read(byte[], int, int) line: 179
NetConn.runRead() line: 1044
NetConn.run() line: 989
Thread.run() line: 744

NetConn.run() line: 989
Thread.run() line: 744

很难详细说明发生了什么,因为 JavaSEPort 的源未绑定(bind)在eclipse环境下

更多信息:我获取了更多资源,可以看到 JavaSEPort.readFromStream 捕获 IOException 并返回 null。再往上几层,在 Socket.read 中有一个 while (buffer==null) 永远不会被转义。这完全解释了这种行为 - 我看到一个无限循环的堆栈跟踪并且读取永远不会返回。

最佳答案

当出现错误时,您应该得到 connectionError 回调而不是异常。

关于java - 输入套接字意外关闭时吞下 IOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34446014/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com