【发布时间】: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() 将返回 false,socketChannel.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