Guest User

Untitled

a guest
Nov 8th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.88 KB | None | 0 0
  1. package prac;
  2.  
  3. import java.io.*;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. import java.nio.file.Files;
  7. import java.util.ArrayList;
  8. import java.util.*;
  9.  
  10.  
  11. /*
  12. The purpose of the program is to build a
  13. server application.
  14. */
  15.  
  16.  
  17. class User {
  18.  
  19. private String username;
  20. private String password;
  21.  
  22. User(String x, String y){
  23. this.username = x;
  24. this.password = y;
  25. }
  26.  
  27. //boolean values
  28. public boolean usernameAuthentication(String x) {
  29. return x.equals(this.username);
  30. }
  31.  
  32. public boolean passwordAuthentication(String y) {
  33. return y.equals(this.password);
  34. }
  35.  
  36. }
  37.  
  38.  
  39.  
  40. public class Serverapp {
  41.  
  42. public static void main(String[] args) throws IOException {
  43.  
  44. ArrayList<User> userNameAndPassword = null;
  45.  
  46. try {
  47. File inputFile = new File("users.txt");
  48. userNameAndPassword = array_Of_Users(inputFile);
  49. }
  50.  
  51. catch (FileNotFoundException e) {
  52. System.out.println("File not found. Make sure the user txt and root folder are located with the Server executable");
  53. System.exit(1);
  54. }
  55.  
  56. File rootFolder = new File("root");
  57. File[] rootFiles = rootFolder.listFiles();
  58. BufferedReader inputFromClient = null;
  59. PrintWriter outputToClient = null;
  60. Socket socket = null;
  61.  
  62. //The actual connection method
  63. try (ServerSocket serverSocket = new ServerSocket(50000))
  64. {
  65. socket = serverSocket.accept();
  66. inputFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  67. outputToClient = new PrintWriter(socket.getOutputStream(), true);
  68.  
  69. String fileName;
  70.  
  71. System.out.println("Connected");
  72.  
  73. boolean connected = true;
  74. do {
  75. String command = inputFromClient.readLine();
  76. switch (command) {
  77. case "USER":
  78. verifyusername(inputFromClient, outputToClient, userNameAndPassword);
  79. break;
  80. case "PASS":
  81. verifypassword(inputFromClient, outputToClient, userNameAndPassword);
  82. break;
  83. case "LIST":
  84. sendFileList(outputToClient, rootFolder);
  85. break;
  86. case "KILL":
  87. fileName = inputFromClient.readLine();
  88. rootFiles = deleteFile(outputToClient, rootFolder, rootFiles, fileName);
  89. break;
  90. case "DONE":
  91. connected = false;
  92. System.out.println("Client Disconnected");
  93. break;
  94. case "RETR":
  95. fileName = inputFromClient.readLine();
  96. retrieveFile(inputFromClient, outputToClient, rootFiles, fileName, socket);
  97. break;
  98. default:
  99.  
  100. break;
  101. }
  102. } while (connected);
  103. }
  104. catch (IOException e) {
  105. System.out.println(e.getMessage());
  106. e.printStackTrace();
  107. } finally {
  108. if (inputFromClient != null) {
  109. inputFromClient.close();
  110. }
  111. if (outputToClient != null) {
  112. outputToClient.close();
  113. }
  114. if (socket != null) {
  115. socket.close();
  116. }
  117. }
  118. }//close main
  119.  
  120. private static void retrieveFile(BufferedReader inputFromClient, PrintWriter outputToClient, File[] rootFiles, String fileToRetrieve, Socket socket) throws IOException {
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129. String input;
  130. boolean found = false;
  131.  
  132. for (File x : rootFiles) {
  133. String fileName = x.getName();
  134. if (fileToRetrieve.equals(fileName)) {
  135. found = true;
  136. outputToClient.println(true);
  137. outputToClient.println("#" + x.length());
  138. input = inputFromClient.readLine();
  139. if (input.equals("SEND")) {
  140. File fileToSend = new File(fileToRetrieve);
  141.  
  142. byte[] byteArray = new byte[(int)x.length()];
  143.  
  144. FileInputStream fis = new FileInputStream("root/"+fileToSend);
  145.  
  146. // BufferedInputStream inputStream = new BufferedInputStream(fis);
  147. // inputStream.read(byteArray, 0, byteArray.length);
  148. // OutputStream outputStream = null;
  149. // outputStream = socket.getOutputStream();
  150. // outputStream.write(byteArray, 0, byteArray.length);
  151.  
  152.  
  153.  
  154.  
  155. Scanner fileSeek = new Scanner(new File("root/"+fileToSend));
  156. String inf = "";
  157. while (fileSeek.hasNextLine())
  158. inf += fileSeek.nextLine() + "xyz";
  159.  
  160.  
  161. outputToClient.println(inf);
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170. fis.close();
  171. outputToClient.println("+File sent");
  172. break;
  173. } else if (input.equals("STOP")) {
  174. outputToClient.println("+ok, RETR aborted");
  175. break;
  176. }
  177. }
  178. }
  179.  
  180. if(!found){
  181. outputToClient.println(false);
  182. }
  183.  
  184.  
  185. }//close retrieveFile
  186.  
  187.  
  188. private static File[] deleteFile(PrintWriter outputToClient, File rootFolder, File[] rootFiles, String fileToDelete) {
  189.  
  190. boolean deleted = false;
  191.  
  192. for(File x: rootFiles){
  193.  
  194. String fileName = x.getName();
  195.  
  196. if(fileToDelete.equals(fileName)){
  197. deleted = x.delete();
  198. outputToClient.println("+" + fileToDelete + " deleted");
  199. break;
  200. }
  201. }
  202.  
  203. if(!deleted){
  204. outputToClient.println("-" + fileToDelete + " does not exist");
  205. }
  206.  
  207. return rootFolder.listFiles();
  208.  
  209. }
  210.  
  211.  
  212. private static void sendFileList(PrintWriter outputToClient, File folder) {
  213. String[] fileList = folder.list();
  214. outputToClient.println("+root");
  215.  
  216. outputToClient.println(fileList.length);
  217.  
  218. for(String x: fileList){
  219. outputToClient.println(x);
  220. }
  221. }
  222.  
  223. //This splits the line into username and password
  224. //trimmedString[0] is the username, trimmedString[1] is the password.
  225.  
  226. private static ArrayList<User> array_Of_Users(File file) throws IOException {
  227. String inputString;
  228. BufferedReader br = null;
  229. ArrayList<User> userArr = new ArrayList<>();
  230. try {
  231. br = new BufferedReader(new FileReader(file));
  232. } catch (FileNotFoundException e) {
  233. e.printStackTrace();
  234. }
  235. while ((inputString = br.readLine()) != null) {
  236. String[] trimmedString = inputString.trim().split("\\s+");
  237. userArr.add(new User(trimmedString[0], trimmedString[1]));
  238. }
  239. return userArr;
  240. }//close array_Of_Users
  241.  
  242.  
  243. private static void verifypassword(BufferedReader inputFromServer, PrintWriter outputToServer, ArrayList<User> userNameAndPassword) throws IOException {
  244.  
  245. String username = inputFromServer.readLine();
  246. String password = inputFromServer.readLine();
  247.  
  248. for (User list : userNameAndPassword) {
  249. boolean findUser = list.usernameAuthentication(username);
  250.  
  251. if (findUser) {
  252. boolean checkPassword = list.passwordAuthentication(password);
  253.  
  254. if(checkPassword){
  255. outputToServer.println(true);
  256. return;
  257. }
  258. }
  259. }
  260.  
  261. outputToServer.println(false);
  262. }
  263.  
  264. //Verifying the username
  265.  
  266. private static void verifyusername(BufferedReader inputFromServer, PrintWriter outputToServer, ArrayList<User> userNameAndPassword) throws IOException {
  267. //read the username from the server
  268. String username = inputFromServer.readLine();
  269. //for each user
  270. for (User list : userNameAndPassword) {
  271. boolean check = list.usernameAuthentication(username);
  272. //if the username was found, send true.
  273. if (check) {
  274. outputToServer.println(true);
  275. return;
  276. }
  277. }
  278. //if username not found, send false
  279. outputToServer.println(false);
  280. }
  281.  
  282. }//close Server
Add Comment
Please, Sign In to add comment