Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package prac;
- import java.io.*;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.nio.file.Files;
- import java.util.ArrayList;
- import java.util.*;
- /*
- The purpose of the program is to build a
- server application.
- */
- class User {
- private String username;
- private String password;
- User(String x, String y){
- this.username = x;
- this.password = y;
- }
- //boolean values
- public boolean usernameAuthentication(String x) {
- return x.equals(this.username);
- }
- public boolean passwordAuthentication(String y) {
- return y.equals(this.password);
- }
- }
- public class Serverapp {
- public static void main(String[] args) throws IOException {
- ArrayList<User> userNameAndPassword = null;
- try {
- File inputFile = new File("users.txt");
- userNameAndPassword = array_Of_Users(inputFile);
- }
- catch (FileNotFoundException e) {
- System.out.println("File not found. Make sure the user txt and root folder are located with the Server executable");
- System.exit(1);
- }
- File rootFolder = new File("root");
- File[] rootFiles = rootFolder.listFiles();
- BufferedReader inputFromClient = null;
- PrintWriter outputToClient = null;
- Socket socket = null;
- //The actual connection method
- try (ServerSocket serverSocket = new ServerSocket(50000))
- {
- socket = serverSocket.accept();
- inputFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- outputToClient = new PrintWriter(socket.getOutputStream(), true);
- String fileName;
- System.out.println("Connected");
- boolean connected = true;
- do {
- String command = inputFromClient.readLine();
- switch (command) {
- case "USER":
- verifyusername(inputFromClient, outputToClient, userNameAndPassword);
- break;
- case "PASS":
- verifypassword(inputFromClient, outputToClient, userNameAndPassword);
- break;
- case "LIST":
- sendFileList(outputToClient, rootFolder);
- break;
- case "KILL":
- fileName = inputFromClient.readLine();
- rootFiles = deleteFile(outputToClient, rootFolder, rootFiles, fileName);
- break;
- case "DONE":
- connected = false;
- System.out.println("Client Disconnected");
- break;
- case "RETR":
- fileName = inputFromClient.readLine();
- retrieveFile(inputFromClient, outputToClient, rootFiles, fileName, socket);
- break;
- default:
- break;
- }
- } while (connected);
- }
- catch (IOException e) {
- System.out.println(e.getMessage());
- e.printStackTrace();
- } finally {
- if (inputFromClient != null) {
- inputFromClient.close();
- }
- if (outputToClient != null) {
- outputToClient.close();
- }
- if (socket != null) {
- socket.close();
- }
- }
- }//close main
- private static void retrieveFile(BufferedReader inputFromClient, PrintWriter outputToClient, File[] rootFiles, String fileToRetrieve, Socket socket) throws IOException {
- String input;
- boolean found = false;
- for (File x : rootFiles) {
- String fileName = x.getName();
- if (fileToRetrieve.equals(fileName)) {
- found = true;
- outputToClient.println(true);
- outputToClient.println("#" + x.length());
- input = inputFromClient.readLine();
- if (input.equals("SEND")) {
- File fileToSend = new File(fileToRetrieve);
- byte[] byteArray = new byte[(int)x.length()];
- FileInputStream fis = new FileInputStream("root/"+fileToSend);
- // BufferedInputStream inputStream = new BufferedInputStream(fis);
- // inputStream.read(byteArray, 0, byteArray.length);
- // OutputStream outputStream = null;
- // outputStream = socket.getOutputStream();
- // outputStream.write(byteArray, 0, byteArray.length);
- Scanner fileSeek = new Scanner(new File("root/"+fileToSend));
- String inf = "";
- while (fileSeek.hasNextLine())
- inf += fileSeek.nextLine() + "xyz";
- outputToClient.println(inf);
- fis.close();
- outputToClient.println("+File sent");
- break;
- } else if (input.equals("STOP")) {
- outputToClient.println("+ok, RETR aborted");
- break;
- }
- }
- }
- if(!found){
- outputToClient.println(false);
- }
- }//close retrieveFile
- private static File[] deleteFile(PrintWriter outputToClient, File rootFolder, File[] rootFiles, String fileToDelete) {
- boolean deleted = false;
- for(File x: rootFiles){
- String fileName = x.getName();
- if(fileToDelete.equals(fileName)){
- deleted = x.delete();
- outputToClient.println("+" + fileToDelete + " deleted");
- break;
- }
- }
- if(!deleted){
- outputToClient.println("-" + fileToDelete + " does not exist");
- }
- return rootFolder.listFiles();
- }
- private static void sendFileList(PrintWriter outputToClient, File folder) {
- String[] fileList = folder.list();
- outputToClient.println("+root");
- outputToClient.println(fileList.length);
- for(String x: fileList){
- outputToClient.println(x);
- }
- }
- //This splits the line into username and password
- //trimmedString[0] is the username, trimmedString[1] is the password.
- private static ArrayList<User> array_Of_Users(File file) throws IOException {
- String inputString;
- BufferedReader br = null;
- ArrayList<User> userArr = new ArrayList<>();
- try {
- br = new BufferedReader(new FileReader(file));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- while ((inputString = br.readLine()) != null) {
- String[] trimmedString = inputString.trim().split("\\s+");
- userArr.add(new User(trimmedString[0], trimmedString[1]));
- }
- return userArr;
- }//close array_Of_Users
- private static void verifypassword(BufferedReader inputFromServer, PrintWriter outputToServer, ArrayList<User> userNameAndPassword) throws IOException {
- String username = inputFromServer.readLine();
- String password = inputFromServer.readLine();
- for (User list : userNameAndPassword) {
- boolean findUser = list.usernameAuthentication(username);
- if (findUser) {
- boolean checkPassword = list.passwordAuthentication(password);
- if(checkPassword){
- outputToServer.println(true);
- return;
- }
- }
- }
- outputToServer.println(false);
- }
- //Verifying the username
- private static void verifyusername(BufferedReader inputFromServer, PrintWriter outputToServer, ArrayList<User> userNameAndPassword) throws IOException {
- //read the username from the server
- String username = inputFromServer.readLine();
- //for each user
- for (User list : userNameAndPassword) {
- boolean check = list.usernameAuthentication(username);
- //if the username was found, send true.
- if (check) {
- outputToServer.println(true);
- return;
- }
- }
- //if username not found, send false
- outputToServer.println(false);
- }
- }//close Server
Add Comment
Please, Sign In to add comment