简体   繁体   English

如果我有1个服务器和多个客户端,是否需要SocketServerChannel?

[英]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.

相关问题 如何将多个客户端连接到单个服务器? - How do I connect multiple clients to a single server? 我正在尝试在单个服务器和多个客户端中使用套接字编程开发聊天应用程序 - I am trying to develop a chat application using socket Programming in single server and multiple Clients 我在将文本读入数组时遇到麻烦,我需要在此代码中进行哪些更改? - I am having troubles reading text into an array, what do I need to change in this code? 我是否需要在服务器端依赖“jersey-client”来支持用它实现的客户端? - Do I need the "jersey-client" dependency on server side to support clients implemented with it? Java-为多个客户端创建一个聊天服务器-我需要多少个线程? - Java - Making a chatserver for multiple clients - how many threads do i need? 我在序列化方面遇到麻烦,我需要在Java中实现它 - I am having trouble with serialization, i need to implement it in java 如何在Java客户端服务器程序中将消息发送到多个客户端 - How do i send the message to multiple clients in java Client-Server Program 如何用一台服务器做多个客户端? - How to do multiple clients with 1 server? 让多个客户端连接到Java Server - Having multiple clients connecting to Java Server 客户端是否需要多线程同时通过套接字接收消息? - Do I need multithreading for clients simultaneously receiving a message via a socket?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM