出于某种原因,当我运行我的服务器代码时,服务器窗口会弹出,但随后它会冻结并且不允许我做任何事情。我已经对我的代码进行了试验,似乎while
循环是问题所在,因为当我删除它时,它工作正常。但我需要while
为许多客户提供服务的循环。这是代码:
注意:该变量numClients
已经在其他地方初始化和实例化。
try{
ServerSocket serverSocket = new ServerSocket(8000);
textArea.append("Server started on " + new Date() + "\n");
while(numClients <= 5){
Socket socket = serverSocket.accept();
InetAddress inetAddress = socket.getInetAddress();
textArea.append("Client \"" + inetAddress.getHostName() + "\" has
connected!");
textArea.append("The IP address of \"" + inetAddress.getHostName() +
"\" is " + inetAddress.getHostAddress());
HandleClient task = new HandleClient(socket);
new Thread(task).start();
numClients++;
}
}
catch(IOException ex){
ex.printStackTrace();
}