繁体   English   中英

非阻塞ServerSocketChannel

[英]Non Blocking ServerSocketChannel

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 );
  }
}

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

否。它已经连接。 OP_CONNECT适用于客户端。

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

正确。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM