jsmith1016

Untitled

Nov 2nd, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.62 KB | None | 0 0
  1. package com.jamessmith;
  2.  
  3. import java.io.*;
  4. import java.net.Socket;
  5. import java.util.Scanner;
  6.  
  7. public class Client {
  8.  
  9.     public static void main(String[] args) throws IOException {
  10.  
  11.  
  12.         FileOutputStream fos;
  13.         BufferedOutputStream bos;
  14.         try (Socket socket = new Socket("localhost", 5001); PrintWriter outputToServer = new PrintWriter(socket.getOutputStream(), true); BufferedReader inputFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream())); Scanner scanner = new Scanner(System.in)) {
  15.             boolean acceptedUserName = false;
  16.             boolean acceptedPassword = false;
  17.             boolean connected = true;
  18.             boolean fileFound;
  19.  
  20.             int bytesRead;
  21.             int current = 0;
  22.             fos = null;
  23.             bos = null;
  24.  
  25.             String currentUser = null;
  26.             System.out.println("+Hello from VSFTP Service!");
  27.             String[] input = scanner.nextLine().trim().split("\\s+");
  28.             String[] command = checkInput(input);
  29.             int numberOfFiles;
  30.             do {
  31.                 switch (command[0]) {
  32.                     case "USER":
  33.                         outputToServer.println(command[0]);
  34.                         acceptedUserName = validateUsername(inputFromServer, outputToServer, command[1]);
  35.                         if (acceptedUserName && !acceptedPassword) {
  36.                             currentUser = command[1];
  37.                             System.out.println("+User-id valid, send password ");
  38.                             input = scanner.nextLine().trim().split("\\s+");
  39.                             command = checkInput(input);
  40.                         } else if (acceptedUserName) {
  41.                             System.out.println("-Already logged-in ");
  42.                             input = scanner.nextLine().trim().split("\\s+");
  43.                             command = checkInput(input);
  44.                         } else {
  45.                             System.out.println("-Invalid user-id ");
  46.                             input = scanner.nextLine().trim().split("\\s+");
  47.                             command = checkInput(input);
  48.                         }
  49.                         break;
  50.  
  51.                     case "PASS":
  52.                         if (acceptedUserName) {
  53.                             outputToServer.println(command[0]);
  54.                             acceptedPassword = validatePassword(inputFromServer, outputToServer, currentUser, command[1]);
  55.                             if (acceptedPassword) {
  56.                                 System.out.println("! Logged in");
  57.                                 System.out.println("+Password is ok and you can begin file transfers");
  58.                                 input = scanner.nextLine().trim().split("\\s+");
  59.                                 command = checkInput(input);
  60.                             } else {
  61.                                 System.out.println("-Wrong password ");
  62.                                 input = scanner.nextLine().trim().split("\\s+");
  63.                                 command = checkInput(input);
  64.                             }
  65.                         } else {
  66.                             System.out.println("-Must enter a valid username before password verification ");
  67.                             input = scanner.nextLine().trim().split("\\s+");
  68.                             command = checkInput(input);
  69.                         }
  70.                         break;
  71.  
  72.                     case "LIST":
  73.                         if (acceptedUserName && acceptedPassword) {
  74.                             outputToServer.println(command[0]);
  75.                             //print directory name
  76.                             System.out.println(inputFromServer.readLine());
  77.                             //get number of files in directory
  78.                             numberOfFiles = Integer.parseInt(inputFromServer.readLine());
  79.                             //retrieve file names from server and print
  80.                             for (int i = 0; i < numberOfFiles; i++) {
  81.                                 System.out.println(inputFromServer.readLine());
  82.                             }
  83.                             input = scanner.nextLine().trim().split("\\s+");
  84.                             command = checkInput(input);
  85.                         } else {
  86.                             System.out.println("-Valid credentials needed to use this command ");
  87.                             input = scanner.nextLine().trim().split("\\s+");
  88.                             command = checkInput(input);
  89.                         }
  90.                         break;
  91.  
  92.                     case "KILL":
  93.                         if (acceptedUserName && acceptedPassword) {
  94.                             outputToServer.println(command[0]);
  95.                             outputToServer.println(command[1]);
  96.                             System.out.println(inputFromServer.readLine());
  97.                             input = scanner.nextLine().trim().split("\\s+");
  98.                             command = checkInput(input);
  99.                         } else {
  100.                             System.out.println("-Valid credentials needed to use this command ");
  101.                             input = scanner.nextLine().trim().split("\\s+");
  102.                             command = checkInput(input);
  103.                         }
  104.                         break;
  105.  
  106.                     case "DONE":
  107.                         outputToServer.println(command[0]);
  108.                         connected = false;
  109.                         break;
  110.  
  111.                     case "RETR":
  112.                         if (acceptedUserName && acceptedPassword) {
  113.                             outputToServer.println(command[0]);
  114.                             outputToServer.println(command[1]);
  115.                             fileFound = Boolean.parseBoolean(inputFromServer.readLine());
  116.                             if (fileFound) {
  117.                                 System.out.println(inputFromServer.readLine());
  118.                                 input = scanner.nextLine().trim().split("\\s+");
  119.                                 command = checkInputForRetrieveFunction(input);
  120.                                 outputToServer.println(command[0]);
  121.                                 System.out.println(inputFromServer.readLine());
  122.                                 if (command[0].equals("SEND")) {
  123.                                     byte[] mybytearray = new byte[10000];
  124.                                     InputStream is = socket.getInputStream();
  125.                                     fos = new FileOutputStream("transferredFile");
  126.                                     bos = new BufferedOutputStream(fos);
  127.                                     bytesRead = is.read(mybytearray, 0, mybytearray.length);
  128.                                     current = bytesRead;
  129.  
  130.                                     do {
  131.                                         bytesRead = is.read(mybytearray, current, (mybytearray.length - current));
  132.                                         if (bytesRead >= 0) current += bytesRead;
  133.                                     } while (bytesRead > -1);
  134.  
  135.                                     bos.write(mybytearray, 0, current);
  136.                                     bos.flush();
  137.                                     System.out.println("did it work");
  138.  
  139.                                     input = scanner.nextLine().trim().split("\\s+");
  140.                                     command = checkInput(input);
  141.                                 } else {
  142.                                     System.out.println("-File not found ");
  143.                                     input = scanner.nextLine().trim().split("\\s+");
  144.                                     command = checkInput(input);
  145.                                 }
  146.                 }
  147.                         }else{
  148.                             System.out.println("-Valid credentials needed to use this command ");
  149.                     input = scanner.nextLine().trim().split("\\s+");
  150.                     command = checkInput(input);
  151.                 }
  152.                 break;
  153.  
  154.                 default:
  155.                     System.out.println("-Command not found ");
  156.                     input = scanner.nextLine().trim().split("\\s+");
  157.                     command = checkInput(input);
  158.                     break;
  159.             } }
  160.             while (connected);
  161.         } catch (IOException e) {
  162.             System.out.println("-Connection Error: VSFTP Server is out for lunch");
  163.         }
  164.     }//end main
  165.     //////////////////////////////////////////////////////////////////////////////////////////
  166.  
  167.     private static boolean validateUsername(BufferedReader inputFromServer, PrintWriter outputToServer, String username) throws IOException {
  168.         outputToServer.println(username);
  169.         return Boolean.parseBoolean(inputFromServer.readLine());
  170.     }//end validateUsername
  171.     //////////////////////////////////////////////////////////////////////////////////////////
  172.  
  173.     private static boolean validatePassword(BufferedReader inputFromServer, PrintWriter outputToServer, String username, String password) throws IOException {
  174.         outputToServer.println(username);
  175.         outputToServer.println(password);
  176.         return Boolean.parseBoolean(inputFromServer.readLine());
  177.     }//end validatePassword
  178.     //////////////////////////////////////////////////////////////////////////////////////////
  179.  
  180.     private static String[] checkInput(String[] command){
  181.  
  182.         Scanner scanner = new Scanner(System.in);
  183.  
  184.         do{
  185.             switch (command[0]) {
  186.                 case "USER":
  187.                 case "PASS":
  188.                 case "KILL":
  189.                 case "RETR":
  190.                     if (command.length < 2) {
  191.                         System.out.println("-This command requires additional input.");
  192.                         command = scanner.nextLine().trim().split("\\s+");
  193.                     } else if (command.length > 2) {
  194.                         System.out.println("-Too much input.");
  195.                         command = scanner.nextLine().trim().split("\\s+");
  196.                     } else {
  197.                         return command;
  198.                     }
  199.                     break;
  200.                 case "LIST":
  201.                 case "DONE":
  202.                     if (command.length > 1) {
  203.                         System.out.println("-This command does not accept any additional input.");
  204.                         command = scanner.nextLine().trim().split("\\s+");
  205.                     } else {
  206.                         return command;
  207.                     }
  208.                     break;
  209.                 default:
  210.                     return command;
  211.             }
  212.         }while(true);
  213.  
  214.     }//end checkInput
  215.     /////////////////////////////////////////////////////////////////////////////////////
  216.     private static String[] checkInputForRetrieveFunction(String[] command) {
  217.         Scanner scanner = new Scanner(System.in);
  218.         do{
  219.             if(command[0].equals("SEND") || command[0].equals("STOP")){
  220.                 if(command.length > 1){
  221.                     System.out.println("-This command does not accept any additional input.");
  222.                     command = scanner.nextLine().trim().split("\\s+");
  223.                 }
  224.                 else
  225.                     return command;
  226.             }
  227.             else{
  228.                 System.out.println("-Only SEND and STOP commands available within RETR function ");
  229.                 command = scanner.nextLine().trim().split("\\s+");
  230.             }
  231.  
  232.  
  233.         }while(true);
  234.     }//end checkInputForRetrieveFunction
  235.     //////////////////////////////////////////////////////////////////////////////////////////
  236.  
  237. }//end Client
Add Comment
Please, Sign In to add comment