public void checkCommand() { try { // Create inputstream BufferedReader fromClient = new BufferedReader(new InputStreamReader(socket.getInputStream())); // Create outputstream PrintWriter toClient = new PrintWriter(socket.getOutputStream(), true); toClient.println("Welcome to Chatroom! "); String inputLine; // String read from Client String outputLine; // String send to Client HandleMessage listener = new HandleMessage( socket, users.getName(index)); // to listen if it has message sent by other users new Thread(listener).start(); // Start the thread // Create UserMessage instance, to store and get message passing among users UserMessage usermessage = new UserMessage(); // Create TimeNoActive instance, to find if user did nothing for a period time TimeNoActive timer_noactive = new TimeNoActive(socket); // Begin to detect no action timer_noactive.execute(); toClient.println("> "); // name = users.getName(index); // name of the current user // Begin to serve the clients and accept commands while ((inputLine = fromClient.readLine()) != null) { // System.out.println(inputLine); // Command "whoelse", to see who are now in the chatroom if (inputLine.equals("whoelse")) { // int size = users.getSize(); // get the size of the current users for (int j = 0; j < size; ++j) { if (users.getRecord(j) && j != index) toClient.println(users.getName(j)); } timer_noactive.reset(); // timer_noactive.execute(); } // Command "wholasthr", to see who logined in last hour else if (inputLine.equals("wholasthr")) { // int size = users.getSize(); for (int j = 0; j < size; ++j) { if (users.getLastHour(j) && j != index) toClient.println(users.getName(j)); } timer_noactive.reset(); // timer_noactive.execute(); } // Command "logout" else if (inputLine.equals("logout")) { users.resetRecord(index); // clear the record for the user break; } // Command "broadcast" else if (inputLine.length() > 9 && inputLine.substring(0, 9).equals("broadcast")) { // String name = users.getName(index); // get the user name String message = inputLine.substring(9); // get the message after broadcast // int size = users.getSize(); // get the size of current users for (int i = 0; i < size; ++i) { if (users.getRecord(i) && i != index) { usermessage.addMessage(message, name, users.getName(i)); } } timer_noactive.reset(); // timer_noactive.execute(); } // Command "Private" else if (inputLine.length() > 7 && inputLine.substring(0, 7).equals("message")) { int i = 7; while (inputLine.charAt(i) == ' ' && i < inputLine.length()) // Ignore the blank " " i++; // int size = users.getSize(); int j = 0; for (j = 0; j < size; ++j) { if (users.getRecord(j) && inputLine.indexOf(users.getName(j)) == i) // It does not detect names like "win" break; } // and "windows", just return first find if (j == size) { toClient.println("This user is not online or not exist "); } else { String reciever = users.getName(j); String message = inputLine.substring( i + reciever.length()); // message will stay after "private" and "name" usermessage.addMessage(message, name, reciever); } timer_noactive.reset(); // timer_noactive.execute(); } // Wrong command else { toClient.println("Wrong Command "); timer_noactive.reset(); // timer_noactive.execute(); } toClient.println("> "); } toClient.println("logout"); // act as a flag to stop the reciever of client } catch (IOException e) { System.err.println(e); } }