Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package prac;
- /*
- The purpose of the program is to build a
- client based app that functions as a
- Very Simple Filed Based Transfer Program.
- The program is built around a Switch-case
- statement. While doing research, this seemed
- the mos intuitive.
- */
- import java.io.*;
- import java.net.Socket;
- import java.util.Scanner;
- public class Clientapp {
- public static void main(String[] args) throws IOException {
- FileOutputStream fos;
- BufferedOutputStream bos;
- try (Socket socket = new Socket("localhost", 50000);
- PrintWriter outputToServer = new PrintWriter(socket.getOutputStream(), true);
- BufferedReader inputFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- Scanner scanner = new Scanner(System.in))
- {
- boolean acceptedUserName = false;
- boolean acceptedPassword = false;
- boolean connected = true;
- boolean fileFound;
- int bytesRead;
- int current = 0;
- fos = null;
- bos = null;
- String currentUser = null;
- System.out.println("+Hello from VSFTP Service!");
- String[] input = scanner.nextLine().trim().split("\\s+");
- String[] command = checkInput(input);
- int numberOfFiles;
- do {
- /* This is a switch case which defines and excecutes the commands*/
- switch (command[0]) {
- //The "USER" command
- case "USER":
- outputToServer.println(command[0]);
- acceptedUserName = verifyusername(inputFromServer, outputToServer, command[1]);
- if (acceptedUserName && !acceptedPassword) {
- currentUser = command[1];
- System.out.println("+User-id valid, send password ");
- input = scanner.nextLine().trim().split("\\s+");
- command = checkInput(input);
- } else if (acceptedUserName) {
- System.out.println("-Already logged-in ");
- input = scanner.nextLine().trim().split("\\s+");
- command = checkInput(input);
- } else {
- System.out.println("-Invalid user-id ");
- input = scanner.nextLine().trim().split("\\s+");
- command = checkInput(input);
- }
- break;
- case "PASS":
- if (acceptedUserName) {
- //The "PASS" command
- outputToServer.println(command[0]);
- acceptedPassword = verifypassword(inputFromServer, outputToServer, currentUser, command[1]);
- if (acceptedPassword) {
- System.out.println("! Logged in");
- System.out.println("+Password is ok and you can begin file transfers");
- input = scanner.nextLine().trim().split("\\s+");
- command = checkInput(input);
- } else {
- System.out.println("-Wrong password ");
- input = scanner.nextLine().trim().split("\\s+");
- command = checkInput(input);
- }
- } else {
- System.out.println("-Must enter a valid username before password verification ");
- input = scanner.nextLine().trim().split("\\s+");
- command = checkInput(input);
- }
- break;
- case "LIST":
- //The "LIST" command
- if (acceptedUserName && acceptedPassword) {
- outputToServer.println(command[0]);
- System.out.println(inputFromServer.readLine());
- numberOfFiles = Integer.parseInt(inputFromServer.readLine());
- for (int i = 0; i < numberOfFiles; i++) {
- System.out.println(inputFromServer.readLine());
- }
- input = scanner.nextLine().trim().split("\\s+");
- command = checkInput(input);
- }
- else {
- System.out.println("-Valid credentials needed to use this command ");
- input = scanner.nextLine().trim().split("\\s+");
- command = checkInput(input);
- }
- break;
- case "KILL":
- //The "KILL" command
- if (acceptedUserName && acceptedPassword) {
- outputToServer.println(command[0]);
- outputToServer.println(command[1]);
- System.out.println(inputFromServer.readLine());
- input = scanner.nextLine().trim().split("\\s+");
- command = checkInput(input);
- }
- else {
- System.out.println("-Valid credentials needed to use this command ");
- input = scanner.nextLine().trim().split("\\s+");
- command = checkInput(input);
- }
- break;
- case "DONE":
- outputToServer.println(command[0]);
- connected = false;
- break;
- case "RETR":
- if (acceptedUserName && acceptedPassword) {
- outputToServer.println(command[0]);
- outputToServer.println(command[1]);
- fileFound = Boolean.parseBoolean(inputFromServer.readLine());
- if (fileFound) {
- System.out.println(inputFromServer.readLine());
- input = scanner.nextLine().trim().split("\\s+");
- command = checkInputForRetrieveFunction(input);
- outputToServer.println(command[0]);
- String xlt = inputFromServer.readLine();
- if (command[0].equals("SEND")) {
- // byte[] mybytearray = new byte[10000];
- // InputStream is = socket.getInputStream();
- System.out.println("Enter the desired filename: ");
- Scanner fln = new Scanner(System.in);
- String xpz = fln.nextLine();
- fos = new FileOutputStream(xpz);
- // bos = new BufferedOutputStream(fos);
- // bytesRead = is.read(mybytearray, 0, mybytearray.length);
- // current = bytesRead;
- PrintWriter out = new PrintWriter(xpz);
- xlt = xlt.replace("+File sent","");
- String[] xltDel = xlt.split("xyz");
- for (int i = 0; i<xltDel.length;i++)
- out.println(xltDel[i]);
- System.out.println("test2");
- out.close();
- // do {
- // bytesRead = is.read(mybytearray, current, (mybytearray.length - current));
- // if (bytesRead >= 0) current += bytesRead;
- // } while (bytesRead > -1);
- // bos.write(inputFromServer);
- // bos.flush();
- input = scanner.nextLine().trim().split("\\s+");
- command = checkInput(input);
- } else {
- System.out.println("-File not found ");
- input = scanner.nextLine().trim().split("\\s+");
- command = checkInput(input);
- }
- }
- }
- else{
- System.out.println("-Correct credentials needed");
- input = scanner.nextLine().trim().split("\\s+");
- command = checkInput(input);
- }
- break;
- default:
- System.out.println("-Command not found ");
- input = scanner.nextLine().trim().split("\\s+");
- command = checkInput(input);
- break;
- } }
- while (connected);
- }
- catch (IOException e) {
- System.out.println("-Connection Error");
- }
- }//close main
- private static boolean verifyusername(BufferedReader inputFromServer, PrintWriter outputToServer, String username) throws IOException {
- outputToServer.println(username);
- return Boolean.parseBoolean(inputFromServer.readLine());
- }//close verifyusername
- private static boolean verifypassword(BufferedReader inputFromServer, PrintWriter outputToServer, String username, String password) throws IOException {
- outputToServer.println(username);
- outputToServer.println(password);
- return Boolean.parseBoolean(inputFromServer.readLine());
- }//close verifypassword
- private static String[] checkInput(String[] command){
- Scanner scanner = new Scanner(System.in);
- do{
- switch (command[0]) {
- case "USER":
- case "PASS":
- case "KILL":
- case "RETR":
- if (command.length < 2) {
- System.out.println("-This command requires additional input.");
- command = scanner.nextLine().trim().split("\\s+");
- } else if (command.length > 2) {
- System.out.println("-Too much input.");
- command = scanner.nextLine().trim().split("\\s+");
- } else {
- return command;
- }
- break;
- case "LIST":
- case "DONE":
- if (command.length > 1) {
- System.out.println("-This command does not accept any additional input.");
- command = scanner.nextLine().trim().split("\\s+");
- } else {
- return command;
- }
- break;
- default:
- return command;
- }
- }while(true);
- }//close checkInput
- private static String[] checkInputForRetrieveFunction(String[] command) {
- Scanner scanner = new Scanner(System.in);
- do{
- if(command[0].equals("SEND") || command[0].equals("STOP")){
- if(command.length > 1){
- System.out.println("-This command does not accept any additional input.");
- command = scanner.nextLine().trim().split("\\s+");
- }
- else
- return command;
- }
- else{
- System.out.println("-Only SEND and STOP commands available within RETR function ");
- command = scanner.nextLine().trim().split("\\s+");
- }
- }while(true);
- }//close checkInputForRetrieveFunction
- }//close Client
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement