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 );
}
}
答案 0 :(得分:2)
如果非阻止
ServerSocketChannel
在调用SocketChannel,
后返回socketChannel.configureBlocking(false),
,则需要使用SocketChannel
注册socketChannel.register(selector,SelectionKey.OP_CONNECT,new ConnectionHandler)
吗?
没有。它已经连接好了。 OP_CONNECT适用于客户。
我认为一旦返回新的
SocketChannel
,它已经连接到远程端点,socketChannel.isConnectionPending()
将返回false,socketChannel.isConnected()
将返回true。
正确。