I couldn't find a similar post here, so here it is:
When I start the server and a couple of clients, I want the users on all clients to be able to input data to be processed. Also, I want the server to be able to exit the listening loop once all client sessions are terminated.
Server code listening loop:
//Console console = System.console();
// or
//InputStreamReader str = new InputStreamReader(System.in);
//BufferedReader uinp = new BufferedReader(str);
// ...
try (ServerSocket serverSocket = new ServerSocket(portNumber)) {
while (listening) {
new SocketServerThread(serverSocket.accept()).start();
//String serverInput = console.readLine("> ");
//if ( serverInput.equalsIgnoreCase("exit") ) { listening = false; }
// or
//System.out.print("> ");
//if ( (uinp.readLine()).equalsIgnoreCase("exit") ) { listening = false; }
}
} catch (IOException e) {
System.err.println("Could not listen on port " + portNumber);
System.exit(-1);
}
Here's the thing: 1) If I code the server to be able to receive input and process it (like an "exit" string to tell the server to exit the listening loop), it seems to me that the second client's thread won't run, which I find quite peculiar. 2) If I don't code the server to be able to exit the listening loop, how can the server exit the loop at the user's will?
As you can see, I've tried (a) Console and (b) InputStreamReader and BufferedReader, but none of them worked.
So, my question is this: how can I have the server quit the listening loop at the user's will and allow the second client's thread run at the same time?