ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java NIO非阻塞:如何拒绝传入连接?

2019-07-09 08:59:53  阅读:195  来源: 互联网

标签:java nonblocking connection nio


我正在尝试使用基于来自’The Rox Java NIO Tutorial’的java NIO(非阻塞)的服务器端代码.有很多传入的套接字连接,我只想接受100.所以如果有100个活动连接,那么新的连接应该被拒绝/拒绝.但是怎么做呢?只有方法ServerSocketChannel.accept()返回SocketChannel对象.使用该对象我可以调用socketChannel.socket().close(),但是连接已经打开.以下是代码的一部分:

@Override
public void run() {
    while (true) {
        try {
            // Wait for an event one of the registered channels
            this.selector.select();

            // Iterate over the set of keys for which events are available
            Iterator selectedKeys = this.selector.selectedKeys().iterator();
            while (selectedKeys.hasNext()) {
                SelectionKey key = (SelectionKey) selectedKeys.next();
                selectedKeys.remove();

                if (!key.isValid()) {
                    continue;
                }

                // Check what event is available and deal with it
                if (key.isAcceptable()) {
                    this.accept(key);
                } else if (key.isReadable()) {
                    this.read(key);
                } else if (key.isWritable()) {
                    this.write(key);
                }
            }
        } catch (Exception e) {
            logger.warn("Reading data", e);
        }
    }
}

并接受()方法:

 private void accept(SelectionKey key) throws IOException {
    // For an accept to be pending the channel must be a server socket channel.
    ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();

    // Accept the connection and make it non-blocking        
    if (noOfConnections < MAX_CONNECTIONS) {
        SocketChannel socketChannel = serverSocketChannel.accept();
        Socket socket = socketChannel.socket();
        socket.setKeepAlive(true);
        socketChannel.configureBlocking(false);
        // Register the new SocketChannel with our Selector, indicating
        // we'd like to be notified when there's data waiting to be read
        socketChannel.register(this.selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);//listener for incoming data: READ from client, WRITE to client
        noOfConnections++;
        logger.info("Accepted: " + socket.getRemoteSocketAddress().toString());
    } else {

        // REJECT INCOMING CONNECTION, but how?

        logger.warn("Server is full: " + noOfConnections + " / " + MAX_CONNECTIONS);
    }
}

如果不接受连接,则反复调用accept()方法.

感谢帮助!

解决方法:

没有办法实现这一点,但我怀疑这是你真正想要的,或者至少你应该做的事情.

如果要停止接受连接,请将服务器套接字通道的选择键中的interestOps更改为零,并在准备好再次接受时将其更改回OP_ACCEPT.在过渡期间,isAcceptable()永远不会成立,因此您描述的问题不会发生.

然而,这不会导致进一步的连接被拒绝:它只会将它们留在积压队列中,在我看来,它们属于TCP的设计者.如果积压队列填满,则会出现另一种失败行为:它在客户端中的影响取决于系统:连接拒绝和/或超时.

标签:java,nonblocking,connection,nio
来源: https://codeday.me/bug/20190709/1411486.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有