将阻塞 Socket 对象转换为 SocketChannel 的套接字?
这听起来可能很奇怪。我创建了一个基于每个套接字一个线程结构的游戏服务器(是的,每个用户只有一个线程;响应由工作线程发送到客户端)。我生成的线程首先对用户进行身份验证并处理登录,之后它仅接收数据并将其排队等待处理。身份验证部分非常关键,需要很长时间才能实现,并且再次实现也需要同样长的时间,所以我在想,身份验证完成后,我是否可以将我的阻塞套接字转换为SocketChannel的套接字,以便选择器可以以非阻塞方式处理其消息?
This might sound weird. I have created a game server based on a thread per socket structure (yes, only one thread per user; responses are sent to clients by worker threads). The thread that I spawn first authenticates the user and handles log-in, after that it only receives data and queues it up for processing. The authentication part is very critical and took a long time to implement, and would take as long to implement again, so I was thinking, after authentication's finished, can I convert the blocking socket that I have into a SocketChannel's socket so that a Selector can handle its messages in a non-blocking manner?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能。您必须从 SocketChannel 开始,然后您可以在阻塞模式下使用套接字(),或者在阻塞或非阻塞模式下使用通道本身。
You can't. You have to start with a SocketChannel, then you can use the socket() from that in blocking mode, or the channel itself in either blocking or non-blocking mode.