 /** Version 1.0
 **/

import java.io.*;
import java.net.*;

public class vkClient {
    public static void main(String[] args) throws IOException {

        if (args.length != 2) {
            System.err.println(
                "Usage: java vkClient <host name> <port number>");
            System.exit(1);
        }

        String hostName = args[0];
        int portNumber = Integer.parseInt(args[1]);

        try (
            Socket kkSocket = new Socket(hostName, portNumber); //Connect to server
            PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); //Message that want to send to server
            BufferedReader in = new BufferedReader(
                new InputStreamReader(kkSocket.getInputStream()));
        ) {
          //Input from user
            BufferedReader stdIn =
                new BufferedReader(new InputStreamReader(System.in));
            String fromServer;
            String fromUser;
            int stateCounter = 0; //To identify the state of the server

            while ((fromServer = in.readLine()) != null) {

                //If the server send back the resource list, tokenize it and display it to the user
                if (stateCounter == 1){
                  String [] resources = fromServer.split("_");
                  fromServer = "";
                  for(int i=0;i<resources.length;i++){
                    fromServer = fromServer + Integer.toString(i+1) + ". " + resources[i] + " "; //Combine the message
                  }
                }

                //Show server's message
                System.out.println("Server: " + fromServer + "\n");
                if (fromServer.equals("Bye."))
                    break;

                //If the state is downloading, wait for awhile for the program to finish download and send a message to the server.
                //HARDCODE
                if(stateCounter == 2){
                  System.out.println("Server: Downloading.. Please wait...\n");
                  try{
                    Thread.sleep(1000);
                  }
                  catch(InterruptedException ex){
                    Thread.currentThread().interrupt();
                  }
                  out.println("Finished downloading.");
                  stateCounter++;
                  continue;
                }

                //Read user input
                System.out.print("Client: ");
                fromUser = stdIn.readLine();

                if (fromUser != null) {
                    //System.out.println("Client: " + fromUser);
                    out.println(fromUser);
                    //If the user selection is invalid, DO NOT increase the counter or change the state of server.
                    if(fromServer.equals("Invalid selection! Please choose again.")){}
                    else
                      stateCounter++;
                }

                //Reset the counter to 1. Server have only 5 states and user want to download other resources.
                if (stateCounter == 4 && fromUser.equalsIgnoreCase("enter")){
                  stateCounter = 1;
                }
            }
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host " + hostName);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to " +
                hostName);
            System.exit(1);
        }
    }
}

@@ -6,17 +6,17 @@
 Version 
-1
+2
 .0%0A **/%0A
@@ -397,24 +397,183 @@
 (args%5B1%5D);%0A%0A
+        String errorMessage = %22Invalid input! Please try again!%22;%0A        String previousMessage = %22%22; // To display again the message if any error in input.%0A%0A
         try 
@@ -1233,19 +1233,442 @@
 null) %7B%0A
-%0A
   
+            String %5B%5D messageFromServer = fromServer.split(%22_%22);%0A              int stateOfServer = Integer.parseInt(messageFromServer%5B0%5D);%0A              fromServer = messageFromServer%5B1%5D;%0A%0A              if (fromServer.equalsIgnoreCase(errorMessage))%7B%0A                System.out.println(%22%5CnServer: %22 + fromServer );%0A                System.out.println(%22Server: %22 + previousMessage + %22%5Cn%22);%0A              %7D%0A              else%7B%0A
         
@@ -1786,20 +1786,21 @@
 tate
-Count
+OfServ
 er == 
-1
+2
 )%7B%0A 
@@ -1820,51 +1820,25 @@
     
-String %5B%5D resources = fromServer.split(%22_%22)
+fromServer = %22%5Cn%22
 ;%0A  
@@ -1857,23 +1857,23 @@
     
-fromServ
+int count
 er = 
-%22%22
+1
 ;%0A  
@@ -1902,32 +1902,45 @@
 t i=
-0;i%3Cresources
+1;i%3CmessageFromServer
 .length
+ - 1
 ;i+
-+
+=2
 )%7B%0A 
@@ -2005,11 +2005,15 @@
 ing(
-i+1
+counter
 ) + 
@@ -2023,26 +2023,68 @@
 %22 + 
-resources%5Bi%5D + %22 %22
+messageFromServer%5Bi%5D + %22 %22 + messageFromServer%5Bi+1%5D + %22%5Cn%22  
 ; //
@@ -2095,32 +2095,64 @@
 ine the message%0A
+                    counter ++;%0A
                 
@@ -2166,32 +2166,78 @@
              %7D%0A%0A
+                previousMessage = fromServer;%0A
                 
@@ -2288,32 +2288,34 @@
 em.out.println(%22
+%5Cn
 Server: %22 + from
@@ -2592,20 +2592,21 @@
 tate
-Count
+OfServ
 er == 
-2
+3
 )%7B%0A 
@@ -2634,32 +2634,34 @@
 em.out.println(%22
+%5Cn
 Server: Download
@@ -2955,42 +2955,8 @@
 %22);%0A
-                  stateCounter++;%0A
     
@@ -2988,32 +2988,48 @@
                %7D
+%0A              %7D
 %0A%0A              
@@ -3292,517 +3292,8 @@
 r);%0A
-                    //If the user selection is invalid, DO NOT increase the counter or change the state of server.%0A                    if(fromServer.equals(%22Invalid selection! Please choose again.%22))%7B%7D%0A                    else%0A                      stateCounter++;%0A                %7D%0A%0A                //Reset the counter to 1. Server have only 5 states and user want to download other resources.%0A                if (stateCounter == 4 && fromUser.equalsIgnoreCase(%22enter%22))%7B%0A                  stateCounter = 1;%0A
     

Version 2