让侦听器使用AsynchronousServerSocketChannel实现。我打开通道,绑定到端口,开始接受连接。我能够听和读这些信息。关闭通道后的问题事件--我的侦听器仍在接受数据。
我关闭服务器通道套接字连接如下。
this.serverChannel.close();
this.serverChannel = null;以下是接受连接的完成处理程序
this.serverChannel
.accept(null,
new java.nio.channels.CompletionHandler<AsynchronousSocketChannel, Void>() {
@Override
public void completed(
AsynchronousSocketChannel result,
Void attachment) {
acceptConnections();
read(new HSLParsingContext(), result);
}
@Override
public void failed(Throwable exc, Void attachment) {
if (!(exc instanceof AsynchronousCloseException))
});
public void read(final HSLParsingContext context,
final AsynchronousSocketChannel channel) {
try {
channel.read(context.buffer, null,
new java.nio.channels.CompletionHandler<Integer, Void>() {
@Override
public void completed(Integer bytesRead, Void attachment) {
try {
HSLSysLogTcpListener.this.logger.infoFmt(
"Bytes received: %s ", bytesRead);
if (bytesRead > 0) {
messageHandler(context, false);
}
if (bytesRead == -1) {
// when nothing is there to read in the
// channel make sure there
// is nothing in the content buffer before
// closing the channel
if (context.content.length() > 0) {
messageHandler(context, true);
}
throw new EOFException();
}
// keep reading data asynchronously
read(context, channel);
} catch (Exception e) {
failed(e, null);
}
}
@Override
public void failed(Throwable e, Void attachment) {
logReadFailureAndClose(channel, e);
}
});
} catch (Throwable e) {
logReadFailureAndClose(channel, e);
}我觉得AsynchronousSocketChannel比打开的接受方法不是封闭的。因此,在关闭AsynchronousServerSocketChannel之后,我将重新考虑消息事件。
有什么办法在关闭AsynchronousSocketChannel的同时关闭serversocketchannel.Please,让我知道最好的方法是完全停止AsynchronousServerSocketChannel,然后通过AsynchronousSocketChannel
发布于 2016-03-23 03:31:01
AsynchronousServerSocketChannel未关闭已建立的连接
我同意。不是的。不是故意的。关闭服务器套接字通道没有理由关闭其他任何内容。
如果您想要关闭已接受的套接字,您必须自己关闭它们,而不是。关闭发生了,或者应该发生,在logReadFailureAndClose()中,您还没有发布。关闭服务器套接字通道与此无关。如果要同时关闭所有内容,则需要一个最新的已接受套接字列表,并在关闭服务器套接字通道时关闭它们。
https://stackoverflow.com/questions/36168790
复制相似问题