Java 连接被远程主机强行关闭
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24125120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connection was forcibly closed by the remote host
提问by Felipe Gutierrez
I have a java.nio.channels.SocketChannel in my jSCSI implamantation that is disconnecting when I try to open a driver with size greater than 4GB. The iscsi RFC says that the BasicHeaderSegment.BHS_FIXED_SIZE may be 48, so with this position I can read the bytes on the channel. The java doc says 5 types of errors, but my app is throwing the last one that doesn't say a specific information.
我的 jSCSI 植入中有一个 java.nio.channels.SocketChannel,当我尝试打开大小大于 4GB 的驱动程序时,它断开连接。iscsi RFC 说 BasicHeaderSegment.BHS_FIXED_SIZE 可能是 48,所以在这个位置我可以读取通道上的字节。Java 文档说 5 种类型的错误,但我的应用程序抛出了最后一个没有说明特定信息的错误。
- NotYetConnectedException
- ClosedChannelException
- AsynchronousCloseException
- ClosedByInterruptException
- IOException
- 未连接异常
- 关闭通道异常
- 异步关闭异常
- ClosedByInterruptException
- IO异常
Code:
代码:
public final int read(final SocketChannel sChannel) throws InternetSCSIException, IOException, DigestException {
// read Basic Header Segment first to determine the total length of this
// Protocol Data Unit.
clear();
final ByteBuffer bhs = ByteBuffer.allocate(BasicHeaderSegment.BHS_FIXED_SIZE);
int len = 0;
while (len < BasicHeaderSegment.BHS_FIXED_SIZE) {
int lens = sChannel.read(bhs);
if (lens == -1) {
// The Channel was closed at the Target (e.g. the Target does
// not support Multiple Connections)
// throw new ClosedChannelException();
return lens;
}
len += lens;
LOGGER.trace("Receiving through SocketChannel: " + len + " of maximal " + BasicHeaderSegment.BHS_FIXED_SIZE);
}
bhs.flip();
error:
错误:
java.io.IOException: An existing connection was forcibly closed by the remote host
at sun.nio.ch.SocketDispatcher.read0(Native Method)
at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
at sun.nio.ch.IOUtil.read(IOUtil.java:197)
at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:379)
at org.jscsi.parser.ProtocolDataUnit.read(ProtocolDataUnit.java:417)
at org.jscsi.target.connection.TargetSenderWorker.receiveFromWire(TargetSenderWorker.java:145)
at org.jscsi.target.connection.Connection$TargetConnection.receivePdu(Connection.java:217)
at org.jscsi.target.connection.phase.TargetFullFeaturePhase.execute(TargetFullFeaturePhase.java:96)
at org.jscsi.target.connection.Connection$TargetConnection.call(Connection.java:264)
at org.jscsi.target.connection.Connection$TargetConnection.call(Connection.java:79)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
[pool-9-thread-1] INFO org.jscsi.target.connection
Thanks in advance, Felipe
提前致谢,费利佩
采纳答案by user207421
Your problem description is incorrect. The SocketChannel isn't closed: the connection is; and it isn't happening when you try to open the connection: it is happening when you read from it.
您的问题描述不正确。SocketChannel 没有关闭:连接是;当您尝试打开连接时不会发生这种情况:当您从中读取时会发生这种情况。
This usually results from sending something after the peer has already closed the connection, which in turn usually means you had previously sent it something it didn't understand.
这通常是因为在对等方已经关闭连接之后发送了一些东西,这通常意味着您之前向它发送了一些它不理解的东西。
回答by Edward Tomasz Napierala
It looks like the other side disconnected you due to some kind of problem. Assuming you're developing the initiator, you should check the target (server) system logs.
看起来另一端由于某种问题而断开了您的连接。假设您正在开发启动器,您应该检查目标(服务器)系统日志。
Can you explain the 4GB comment? I mean, what exactly does "open a driver with size greater than 4GB" mean, what driver?
你能解释一下 4GB 的评论吗?我的意思是,“打开大小大于 4GB 的驱动程序”究竟是什么意思,什么驱动程序?