I have created an arraylist within the server for my system which stores each user's name in it. This is entered in the client, and then sent to the server via a printwriter. However I would like this list to update when a client disconnects from the socket, and remove the user name that they entered. The updated user name list must then be displayed on on both the existing clients and the server.
Server:
do
{
//Wait for client.
client = serverSocket.accept();
in = new BufferedReader(
new InputStreamReader(client.getInputStream()));
out = new PrintWriter(
client.getOutputStream(),true);
userNames.add(in.readLine()); //Adds to the userNames array based read in from the client
System.out.println("---User List---");
for(int size = 0; size < userNames.size(); size++)
{
System.out.println(userNames.get(size));
}
out.println("Welcome, the following users are connected...");
out.println(userNames);
handler = new ClientHandler(client);
handler.start();
}while (true);
Client:
do
{
System.out.print("Please enter a username:");
Users user = new Users(keyboard.nextLine());
output.println(user.username);
System.out.println(in.readLine() + "\n");
System.out.println(in.readLine());
System.out.print("\nEnter message ('QUIT' to exit): ");
message = keyboard.nextLine();
output.println(message);
if (!message.equals("QUIT"))
{
response = networkInput.nextLine();
System.out.println("\n" + response);
output.println(user.username);
}
}while (!message.equals("QUIT"));