我正在开发一个程序,我有一个服务器和客户端类,但目前它只能处理一个客户端。我需要服务器能够使用多线程同时(同时)处理多个客户端。
这是我的服务器代码;如何更改它以同时处理多个客户端?
public static void main(String[] args) throws IOException {
ServerSocket socket = new ServerSocket(8945);
Server serverInstance = new Server();
System.out.println("Server is running. Waiting for client.");
while(true) {
server.socket = s.accept();
System.out.println("Client connected");
serverInstance.run();
System.out.println("Client disconnected. Waiting for new client.");
}
}
public void run() {
try {
try {
in = new Scanner(socket.getInputStream());
out = new PrintWriter(socket.getOutputStream());
RequestHandlingMethod();
} finally {
socket.close();
}
} catch (IOException e) {
System.err.println(e);
}
}
创建一个处理客户端的单独类。让它实现Runnable
,这样你就可以用它开始一个单独的Thread
。
public class ClientHandler implements Runnable {
private final Socket socket;
public ClientHandler(Socket socket) {
this.socket = socket;
}
public void run() {
try (Socket socket = this.socket;
Scanner in = new Scanner(socket.getInputStream();
PrintWriter out = new PrintWriter(socket.getOutputStream()) {
//todo: do whatever you need to do
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println("Client disconnected.");
}
}
然后在您的服务器中执行:
System.out.println("Waiting for new client connection");
Socket clientSocket = s.accept();
System.out.println("Client connected");
new Thread(new ClientHandler(clientSocket)).start();
如果您不想创建大量的一次性线程,您可能需要考虑使用带有缓存线程池的ExecutorService
(或者如果您愿意,可以使用您选择的其他线程池)。
你只需用ExecutorService
创建一个新的ExecutorService executor = ExecutorService.newCachedThreadPool()
然后在你的循环中你做:
System.out.println("Waiting for new client connection");
Socket clientSocket = s.accept();
System.out.println("Client connected");
executor.submit(new ClientHandler(clientSocket));
如果您认为您将拥有大量并发客户端,则可能需要考虑使用带有NIO的非阻塞服务器。它将具有1个单事件循环线程(不会阻塞accept
)并处理其中的所有I / O事件,并且您可以拥有一个工作线程池来执行客户端处理逻辑。