[英]Do I need SocketServerChannel if I am having 1 server and multiple clients?
I am building an Android application which has a server that listens to multiple incoming connections, and also have multiple outgoing connections. 我正在构建一个具有服务器的Android应用程序,该服务器侦听多个传入连接,并且还具有多个传出连接。
I have 1 main activity and 2 helper classes: Server and Client. 我有1个主要活动和2个帮助程序类:服务器和客户端。 They both extend ASyncTask for multithreading.
它们都为多线程扩展了ASyncTask。
I am planning to use Selector for this job. 我正计划使用Selector来完成这项工作。 And so, I am planning to use SocketServerChannel for the server class, while having multiple SocketChannels for my client class.
因此,我计划将SocketServerChannel用于服务器类,同时将多个SocketChannel用于我的客户端类。 These Channels will be handled by the Selector.
这些通道将由选择器处理。
My question is, since I am using 1 single server and ASyncTask, is it redundant to use SocketServerChannel and Selector for my Server class? 我的问题是,由于我正在使用1个单服务器和ASyncTask,因此对我的Server类使用SocketServerChannel和Selector是否多余? Or should I still use it to manage the multiple incoming connections?
还是应该使用它来管理多个传入连接?
EDIT: I found another way to do this. 编辑:我找到了另一种方法来做到这一点。 It is very inefficient, but it gets the work done.
它的效率很低,但是可以完成工作。 I just use normal Sockets and ServerSockets, and for all possible outgoing connections, I create a new Socket that is one-time use only.
我只使用普通的Sockets和ServerSockets,对于所有可能的传出连接,我创建了一个只能使用一次的新Socket。 This way, I don't have to bother with keeping track of all of my connections.
这样,我不必费心跟踪所有连接。
Java.nio通常比使用java.io更复杂,并且在速度和负载方面必定更为优化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.