Advertisement
Guest User

Untitled

a guest
Nov 8th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.33 KB | None | 0 0
  1. package prac;
  2.  
  3. /*
  4. The purpose of the program is to build a
  5. client based app that functions as a
  6. Very Simple Filed Based Transfer Program.
  7. The program is built around a Switch-case
  8. statement. While doing research, this seemed
  9. the mos intuitive.
  10. */
  11.  
  12. import java.io.*;
  13. import java.net.Socket;
  14. import java.util.Scanner;
  15.  
  16. public class Clientapp {
  17.  
  18. public static void main(String[] args) throws IOException {
  19.  
  20.  
  21. FileOutputStream fos;
  22. BufferedOutputStream bos;
  23. try (Socket socket = new Socket("localhost", 50000);
  24. PrintWriter outputToServer = new PrintWriter(socket.getOutputStream(), true);
  25. BufferedReader inputFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  26. Scanner scanner = new Scanner(System.in))
  27. {
  28. boolean acceptedUserName = false;
  29. boolean acceptedPassword = false;
  30. boolean connected = true;
  31. boolean fileFound;
  32.  
  33. int bytesRead;
  34. int current = 0;
  35. fos = null;
  36. bos = null;
  37.  
  38. String currentUser = null;
  39. System.out.println("+Hello from VSFTP Service!");
  40. String[] input = scanner.nextLine().trim().split("\\s+");
  41. String[] command = checkInput(input);
  42. int numberOfFiles;
  43. do {
  44. /* This is a switch case which defines and excecutes the commands*/
  45. switch (command[0]) {
  46.  
  47. //The "USER" command
  48.  
  49. case "USER":
  50. outputToServer.println(command[0]);
  51. acceptedUserName = verifyusername(inputFromServer, outputToServer, command[1]);
  52. if (acceptedUserName && !acceptedPassword) {
  53. currentUser = command[1];
  54. System.out.println("+User-id valid, send password ");
  55. input = scanner.nextLine().trim().split("\\s+");
  56. command = checkInput(input);
  57. } else if (acceptedUserName) {
  58. System.out.println("-Already logged-in ");
  59. input = scanner.nextLine().trim().split("\\s+");
  60. command = checkInput(input);
  61. } else {
  62. System.out.println("-Invalid user-id ");
  63. input = scanner.nextLine().trim().split("\\s+");
  64. command = checkInput(input);
  65. }
  66. break;
  67.  
  68. case "PASS":
  69. if (acceptedUserName) {
  70.  
  71. //The "PASS" command
  72.  
  73. outputToServer.println(command[0]);
  74. acceptedPassword = verifypassword(inputFromServer, outputToServer, currentUser, command[1]);
  75. if (acceptedPassword) {
  76. System.out.println("! Logged in");
  77. System.out.println("+Password is ok and you can begin file transfers");
  78. input = scanner.nextLine().trim().split("\\s+");
  79. command = checkInput(input);
  80. } else {
  81. System.out.println("-Wrong password ");
  82. input = scanner.nextLine().trim().split("\\s+");
  83. command = checkInput(input);
  84. }
  85. } else {
  86. System.out.println("-Must enter a valid username before password verification ");
  87. input = scanner.nextLine().trim().split("\\s+");
  88. command = checkInput(input);
  89. }
  90. break;
  91.  
  92. case "LIST":
  93.  
  94. //The "LIST" command
  95.  
  96. if (acceptedUserName && acceptedPassword) {
  97. outputToServer.println(command[0]);
  98. System.out.println(inputFromServer.readLine());
  99. numberOfFiles = Integer.parseInt(inputFromServer.readLine());
  100. for (int i = 0; i < numberOfFiles; i++) {
  101. System.out.println(inputFromServer.readLine());
  102. }
  103. input = scanner.nextLine().trim().split("\\s+");
  104. command = checkInput(input);
  105. }
  106. else {
  107. System.out.println("-Valid credentials needed to use this command ");
  108. input = scanner.nextLine().trim().split("\\s+");
  109. command = checkInput(input);
  110. }
  111. break;
  112.  
  113. case "KILL":
  114.  
  115. //The "KILL" command
  116.  
  117. if (acceptedUserName && acceptedPassword) {
  118. outputToServer.println(command[0]);
  119. outputToServer.println(command[1]);
  120. System.out.println(inputFromServer.readLine());
  121. input = scanner.nextLine().trim().split("\\s+");
  122. command = checkInput(input);
  123. }
  124. else {
  125. System.out.println("-Valid credentials needed to use this command ");
  126. input = scanner.nextLine().trim().split("\\s+");
  127. command = checkInput(input);
  128. }
  129. break;
  130.  
  131. case "DONE":
  132. outputToServer.println(command[0]);
  133. connected = false;
  134. break;
  135.  
  136. case "RETR":
  137. if (acceptedUserName && acceptedPassword) {
  138. outputToServer.println(command[0]);
  139. outputToServer.println(command[1]);
  140. fileFound = Boolean.parseBoolean(inputFromServer.readLine());
  141. if (fileFound) {
  142.  
  143. System.out.println(inputFromServer.readLine());
  144. input = scanner.nextLine().trim().split("\\s+");
  145. command = checkInputForRetrieveFunction(input);
  146. outputToServer.println(command[0]);
  147. String xlt = inputFromServer.readLine();
  148.  
  149. if (command[0].equals("SEND")) {
  150. // byte[] mybytearray = new byte[10000];
  151. // InputStream is = socket.getInputStream();
  152. System.out.println("Enter the desired filename: ");
  153. Scanner fln = new Scanner(System.in);
  154. String xpz = fln.nextLine();
  155.  
  156.  
  157.  
  158.  
  159. fos = new FileOutputStream(xpz);
  160. // bos = new BufferedOutputStream(fos);
  161. // bytesRead = is.read(mybytearray, 0, mybytearray.length);
  162. // current = bytesRead;
  163. PrintWriter out = new PrintWriter(xpz);
  164.  
  165. xlt = xlt.replace("+File sent","");
  166. String[] xltDel = xlt.split("xyz");
  167.  
  168. for (int i = 0; i<xltDel.length;i++)
  169. out.println(xltDel[i]);
  170. System.out.println("test2");
  171. out.close();
  172. // do {
  173. // bytesRead = is.read(mybytearray, current, (mybytearray.length - current));
  174. // if (bytesRead >= 0) current += bytesRead;
  175. // } while (bytesRead > -1);
  176.  
  177. // bos.write(inputFromServer);
  178. // bos.flush();
  179.  
  180.  
  181. input = scanner.nextLine().trim().split("\\s+");
  182. command = checkInput(input);
  183. } else {
  184. System.out.println("-File not found ");
  185. input = scanner.nextLine().trim().split("\\s+");
  186. command = checkInput(input);
  187. }
  188. }
  189. }
  190. else{
  191. System.out.println("-Correct credentials needed");
  192. input = scanner.nextLine().trim().split("\\s+");
  193. command = checkInput(input);
  194. }
  195. break;
  196.  
  197. default:
  198. System.out.println("-Command not found ");
  199. input = scanner.nextLine().trim().split("\\s+");
  200. command = checkInput(input);
  201. break;
  202. } }
  203. while (connected);
  204. }
  205. catch (IOException e) {
  206. System.out.println("-Connection Error");
  207. }
  208. }//close main
  209.  
  210.  
  211.  
  212. private static boolean verifyusername(BufferedReader inputFromServer, PrintWriter outputToServer, String username) throws IOException {
  213. outputToServer.println(username);
  214. return Boolean.parseBoolean(inputFromServer.readLine());
  215. }//close verifyusername
  216.  
  217.  
  218.  
  219. private static boolean verifypassword(BufferedReader inputFromServer, PrintWriter outputToServer, String username, String password) throws IOException {
  220. outputToServer.println(username);
  221. outputToServer.println(password);
  222. return Boolean.parseBoolean(inputFromServer.readLine());
  223. }//close verifypassword
  224.  
  225.  
  226.  
  227. private static String[] checkInput(String[] command){
  228.  
  229. Scanner scanner = new Scanner(System.in);
  230.  
  231. do{
  232. switch (command[0]) {
  233. case "USER":
  234. case "PASS":
  235. case "KILL":
  236. case "RETR":
  237. if (command.length < 2) {
  238. System.out.println("-This command requires additional input.");
  239. command = scanner.nextLine().trim().split("\\s+");
  240. } else if (command.length > 2) {
  241. System.out.println("-Too much input.");
  242. command = scanner.nextLine().trim().split("\\s+");
  243. } else {
  244. return command;
  245. }
  246. break;
  247. case "LIST":
  248. case "DONE":
  249. if (command.length > 1) {
  250. System.out.println("-This command does not accept any additional input.");
  251. command = scanner.nextLine().trim().split("\\s+");
  252. } else {
  253. return command;
  254. }
  255. break;
  256. default:
  257. return command;
  258. }
  259. }while(true);
  260.  
  261. }//close checkInput
  262.  
  263.  
  264. private static String[] checkInputForRetrieveFunction(String[] command) {
  265. Scanner scanner = new Scanner(System.in);
  266. do{
  267. if(command[0].equals("SEND") || command[0].equals("STOP")){
  268. if(command.length > 1){
  269. System.out.println("-This command does not accept any additional input.");
  270. command = scanner.nextLine().trim().split("\\s+");
  271. }
  272. else
  273. return command;
  274. }
  275. else{
  276. System.out.println("-Only SEND and STOP commands available within RETR function ");
  277. command = scanner.nextLine().trim().split("\\s+");
  278. }
  279.  
  280.  
  281. }while(true);
  282. }//close checkInputForRetrieveFunction
  283.  
  284.  
  285.  
  286. }//close Client
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement