当套接字意外关闭时,此代码段失败。没有
抛出IOException
,s.read()
永不返回。
try {
while (!doneReading)
{ /* read blocks waiting for data */
int readResult = s.read(inBuf, inBufLength, 1);
...
}
}
catch (IOException err) { .. }
最初关闭套接字时,堆栈如下:
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的来源未受约束,因此很难说明发生了什么 在日食环境中
更多信息:我获得了更多的源代码,并且可以看到JavaSEPort.readFromStream捕获IOException并返回null。在Socket.read中有一些更高级别,有一段时间(缓冲区== null)永远不会被转义。这完全解释了行为 - 我看到无限循环的堆栈跟踪和读取从不返回。
答案 0 :(得分:1)
如果出现错误,您应该获得connectionError
回调而不是异常。