0

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"));
DaveDavidson
  • 206
  • 1
  • 8
  • 22
  • 2
    You need to detect socket closure and add reaction in your server for this case. Have a look http://stackoverflow.com/questions/10240694/java-socket-api-how-to-tell-if-a-connection-has-been-closed – Andrey Lebedenko Jul 16 '16 at 15:14
  • That doesnt really help. I need to know to remove a user name from an array list which is located in the server, once the client has closed a connection. **More than one** client can be connected at one time. – DaveDavidson Jul 16 '16 at 16:52

1 Answers1

1

Andrey is correct, you need to detect socket closure. But you need to add a little bit more logic to your code.

I expect the ClientHandler.start() starts a new thread that has a loop and reading/writing data from/to a client. The socket close detection will be probably implemented in this thread. The ClientHandler is not aware about the user user that initiated the connection. So the code could change a little to make the ClientHandler aware about user.

String userName = in.readLine(); 
userNames.add(userName); 
handler = new ClientHandler(client, userName);
handler.start();

or perhaps better - read userName in ClientHandler

handler = new ClientHandler(serverSocket.accept());
...
ClientHandler(ServerSocket s) {
    // get stream and read username here
    // add username to list

You will need a class for users storage that will be synchronized so it allows safe access from multiple ClientHandler instances. When ClientHandler detects disconnection than it can easily remove user name from the storage and initiates triggering users.

Not you will need to store not only names in the user storage because you will need to inform all clients about connection lost so you will need to store list of active ClientHandler instances to inform all clients.

Zaboj Campula
  • 3,155
  • 3
  • 24
  • 42