SocketChannel socketChannel = serverSocketChannel.accept();
当非阻塞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 );
}
}
最佳答案
当非阻塞ServerSocketChannel
调用SocketChannel,
后返回socketChannel.configureBlocking(false),
时,是否需要使用SocketChannel
注册socketChannel.register(selector,SelectionKey.OP_CONNECT,new ConnectionHandler)
?
否。它已经连接。 OP_CONNECT适用于客户端。
我假设一旦返回了新的SocketChannel
,它已经连接到远程端点,并且socketChannel.isConnectionPending()
将返回false,而socketChannel.isConnected()
将返回true。
正确。