【问题标题】:Non Blocking ServerSocketChannel非阻塞 ServerSocketChannel
【发布时间】:2015-05-02 06:44:19
【问题描述】:
SocketChannel socketChannel = serverSocketChannel.accept();

当一个Non-Blocking ServerSocketChannel返回一个SocketChannel,调用socketChannel.configureBlocking(false)后,是否需要再使用socketChannel.register(selector,SelectionKey.OP_CONNECT,new ConnectionHandler)注册SocketChannel?

我假设一旦返回新的 SocketChannel,它已经连接到远程端点,socketChannel.isConnectionPending() 将返回 falsesocketChannel.isConnected() 将返回 true

public class ConnectionHandler
{
  public void handleConnect ( SelectionKey key )
  {
    SocketChannel socketChannel = SocketChannel.class.cast ( key.channel() );
    socketChannel.finishConnect ();

    socketChannel.register ( key.selector (), SelectionKey.OP_READ );
  }
}

【问题讨论】:

    标签: java server nio nonblocking


    【解决方案1】:

    当非阻塞ServerSocketChannel 在调用socketChannel.configureBlocking(false), 后返回SocketChannel, 时,是否需要使用socketChannel.register(selector,SelectionKey.OP_CONNECT,new ConnectionHandler) 注册SocketChannel

    没有。它已经连接了。 OP_CONNECT 用于客户端。

    我假设一旦返回新的SocketChannel,它已经连接到远程端点,socketChannel.isConnectionPending() 将返回 false,socketChannel.isConnected() 将返回 true。

    正确。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-06
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      • 1970-01-01
      • 1970-01-01
      • 2018-03-21
      • 2010-09-09
      相关资源
      最近更新 更多