【发布时间】:2019-02-26 12:09:57
【问题描述】:
我正在开发一个程序,其中有一个服务器和客户端类,但目前它一次只处理一个客户端。 我需要服务器能够使用多线程同时(同时)处理多个客户端。
这是我的服务器代码;如何更改它以同时处理多个客户端?
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);
}
}
【问题讨论】:
标签: java multithreading client-server