SocketChannel socketChannel = serverSocketChannel.accept();非阻塞ServerSocketChannel在调用socketChannel.configureBlocking(false)后返回SocketChannel时,是否需要使用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 );
}
}发布于 2015-03-02 00:25:48
当非阻塞
ServerSocketChannel调用socketChannel.configureBlocking(false),后返回SocketChannel,时,是否需要使用socketChannel.register(selector,SelectionKey.OP_CONNECT,new ConnectionHandler)注册SocketChannel?
不是的。已经连在一起了。OP_CONNECT是给客户的。
我假设,一旦返回了新的
SocketChannel,它已经连接到远程端点,socketChannel.isConnectionPending()将返回false,socketChannel.isConnected()将返回true。
对,是这样。
https://stackoverflow.com/questions/28800912
复制相似问题