非阻塞 ServerSocketChannel
Non Blocking ServerSocketChannel
SocketChannel socketChannel = serverSocketChannel.accept();
当非阻塞ServerSocketChannelreturn是SocketChannel时,调用socketChannel.configureBlocking(false)
后是否需要使用socketChannel.register(selector,SelectionKey.OP_CONNECT,new ConnectionHandler)
注册SocketChannel?
我假设一旦新的 SocketChannel returned 它已经连接到远程端点并且 socketChannel.isConnectionPending()
将 return false
和 socketChannel.isConnected()
将 return 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 );
}
}
When a Non-Blocking ServerSocketChannel
returns a SocketChannel,
after calling socketChannel.configureBlocking(false),
is it required to then register the SocketChannel
using socketChannel.register(selector,SelectionKey.OP_CONNECT,new ConnectionHandler)
?
没有。它已经连接。 OP_CONNECT 是给客户的。
I would assume that once the new SocketChannel
has been returned that it is already connected to the remote endpoint and socketChannel.isConnectionPending()
will return false and socketChannel.isConnected()
will return true.
正确。
SocketChannel socketChannel = serverSocketChannel.accept();
当非阻塞ServerSocketChannelreturn是SocketChannel时,调用socketChannel.configureBlocking(false)
后是否需要使用socketChannel.register(selector,SelectionKey.OP_CONNECT,new ConnectionHandler)
注册SocketChannel?
我假设一旦新的 SocketChannel returned 它已经连接到远程端点并且 socketChannel.isConnectionPending()
将 return false
和 socketChannel.isConnected()
将 return 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 );
}
}
When a Non-Blocking
ServerSocketChannel
returns aSocketChannel,
after callingsocketChannel.configureBlocking(false),
is it required to then register theSocketChannel
usingsocketChannel.register(selector,SelectionKey.OP_CONNECT,new ConnectionHandler)
?
没有。它已经连接。 OP_CONNECT 是给客户的。
I would assume that once the new
SocketChannel
has been returned that it is already connected to the remote endpoint andsocketChannel.isConnectionPending()
will return false andsocketChannel.isConnected()
will return true.
正确。