gpt4 book ai didi

java - 非阻塞ServerSocketChannel

转载 作者:行者123 更新时间:2023-12-02 04:55:34 25 4
gpt4 key购买 nike

SocketChannel socketChannel = serverSocketChannel.accept();

当非阻塞ServerSocketChannel返回SocketChannel时,调用socketChannel.configureBlocking(false)后,是否需要使用socketChannel.register(selector,SelectionKey.OP_CONNECT)注册SocketChannel ,新的ConnectionHandler)

我假设一旦返回新的 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 );
}
}

最佳答案

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.

正确。

关于java - 非阻塞ServerSocketChannel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28800912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com