简体   繁体   English

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

[英]Convert a blocking Socket object to a SocketChannel's socket?

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? 身份验证部分非常关键并且需要花费很长时间才能实现,并且需要花费很长时间再次实现,所以我想,在身份验证完成后,我可以将我拥有的阻塞套接字转换为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. 您必须从SocketChannel开始,然后您可以在阻塞模式下使用socket(),或者在阻塞或非阻塞模式下使用通道本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM