将阻塞 Socket 对象转换为 SocketChannel 的套接字?

发布于 2024-12-17 04:47:56 字数 218 浏览 5 评论 0原文

这听起来可能很奇怪。我创建了一个基于每个套接字一个线程结构的游戏服务器(是的,每个用户只有一个线程;响应由工作线程发送到客户端)。我生成的线程首先对用户进行身份验证并处理登录,之后它仅接收数据并将其排队等待处理。身份验证部分非常关键,需要很长时间才能实现,并且再次实现也需要同样长的时间,所以我在想,身份验证完成后,我是否可以将我的阻塞套接字转换为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送君千里 2024-12-24 04:47:56

你不能。您必须从 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文