首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AsynchronousServerSocketChannel未关闭已建立的连接

AsynchronousServerSocketChannel未关闭已建立的连接
EN

Stack Overflow用户
提问于 2016-03-23 02:33:04
回答 1查看 1.2K关注 0票数 0

让侦听器使用AsynchronousServerSocketChannel实现。我打开通道,绑定到端口,开始接受连接。我能够听和读这些信息。关闭通道后的问题事件--我的侦听器仍在接受数据。

我关闭服务器通道套接字连接如下。

代码语言:javascript
复制
 this.serverChannel.close();
 this.serverChannel = null;

以下是接受连接的完成处理程序

代码语言:javascript
复制
 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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-23 03:31:01

AsynchronousServerSocketChannel未关闭已建立的连接

我同意。不是的。不是故意的。关闭服务器套接字通道没有理由关闭其他任何内容。

如果您想要关闭已接受的套接字,您必须自己关闭它们,而不是。关闭发生了,或者应该发生,在logReadFailureAndClose()中,您还没有发布。关闭服务器套接字通道与此无关。如果要同时关闭所有内容,则需要一个最新的已接受套接字列表,并在关闭服务器套接字通道时关闭它们。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36168790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档