@@ -0,0 +1,3477 @@
+ /** Version 1.0%0A **/%0A%0Aimport java.io.*;%0Aimport java.net.*;%0A%0Apublic class vkClient %7B%0A    public static void main(String%5B%5D args) throws IOException %7B%0A%0A        if (args.length != 2) %7B%0A            System.err.println(%0A                %22Usage: java vkClient %3Chost name%3E %3Cport number%3E%22);%0A            System.exit(1);%0A        %7D%0A%0A        String hostName = args%5B0%5D;%0A        int portNumber = Integer.parseInt(args%5B1%5D);%0A%0A        try (%0A            Socket kkSocket = new Socket(hostName, portNumber); //Connect to server%0A            PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); //Message that want to send to server%0A            BufferedReader in = new BufferedReader(%0A                new InputStreamReader(kkSocket.getInputStream()));%0A        ) %7B%0A          //Input from user%0A            BufferedReader stdIn =%0A                new BufferedReader(new InputStreamReader(System.in));%0A            String fromServer;%0A            String fromUser;%0A            int stateCounter = 0; //To identify the state of the server%0A%0A            while ((fromServer = in.readLine()) != null) %7B%0A%0A                //If the server send back the resource list, tokenize it and display it to the user%0A                if (stateCounter == 1)%7B%0A                  String %5B%5D resources = fromServer.split(%22_%22);%0A                  fromServer = %22%22;%0A                  for(int i=0;i%3Cresources.length;i++)%7B%0A                    fromServer = fromServer + Integer.toString(i+1) + %22. %22 + resources%5Bi%5D + %22 %22; //Combine the message%0A                  %7D%0A                %7D%0A%0A                //Show server's message%0A                System.out.println(%22Server: %22 + fromServer + %22%5Cn%22);%0A                if (fromServer.equals(%22Bye.%22))%0A                    break;%0A%0A                //If the state is downloading, wait for awhile for the program to finish download and send a message to the server.%0A                //HARDCODE%0A                if(stateCounter == 2)%7B%0A                  System.out.println(%22Server: Downloading.. Please wait...%5Cn%22);%0A                  try%7B%0A                    Thread.sleep(1000);%0A                  %7D%0A                  catch(InterruptedException ex)%7B%0A                    Thread.currentThread().interrupt();%0A                  %7D%0A                  out.println(%22Finished downloading.%22);%0A                  stateCounter++;%0A                  continue;%0A                %7D%0A%0A                //Read user input%0A                System.out.print(%22Client: %22);%0A                fromUser = stdIn.readLine();%0A%0A                if (fromUser != null) %7B%0A                    //System.out.println(%22Client: %22 + fromUser);%0A                    out.println(fromUser);%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                %7D%0A            %7D%0A        %7D catch (UnknownHostException e) %7B%0A            System.err.println(%22Don't know about host %22 + hostName);%0A            System.exit(1);%0A        %7D catch (IOException e) %7B%0A            System.err.println(%22Couldn't get I/O for the connection to %22 +%0A                hostName);%0A            System.exit(1);%0A        %7D%0A    %7D%0A%7D%0A

Create vkClient.java