I am creating a server class for a little Instant Messaging app. Everything is working when I try to have the server connect to a single client, but I am attempting to expand it be connect to multiple clients. I have tested and it is able to connect to multiple clients, but the problem lies with the fact that I cannot read inputs from all of them at the same time.
Do I need to open a new thread for each client to constantly be checking for inputs? I feel like this is extremely inefficient.
This is the code that checks for inputs from the clients, which are stored in an Arraylist at the moment. I believe that it is similar to a scanner, where it does not continue the code until there is an input.
private void chat() throws IOException{
String message = " You are now connected! ";
sendMessage(message);
ableToType(true);
do{
try{
//right now it is only checking the first (0) client
message = (String) inputs.get(0).readObject(); // this is the line that checks for inputs
showMessage("\n" + message, new java.awt.Color(20, 124, 34));
}catch(ClassNotFoundException classNotFoundException){
showMessage("\nUnreadable Message Sent by Client");
}
}while(!message.contains("END"));
}