Answer to Question #294518 in Java | JSP | JSF for hummi

Question #294518

LOGIN This is the command a client must initiate in order to gain access to anything on the server. It would be in the following format: C: LOGIN john john22 S: SUCCESS Or, if the login information is incorrect (username or password, or both are wrong): C: LOGIN dude dude111 S: FAILURE: Please provide correct username and password. Try again.

LIST Two options for this command: • With no flags, the LIST command returns a list of all the solutions requested by this particular user (e.g., john will only see the contents of john_solutions.txt) • With the -all flag o If the user is root, who is successfully logged in, the command lists all of the solutions requested (including root’s own) from all of the files, organized by username A regular (flagless) interaction might look like the following (see above commands issued under 


1
Expert's answer
2022-02-06T17:17:35-0500
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static final int SERVER_PORT = 2022;
    static String logout = "";

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Domain:");
        String host = in.nextLine();
        try (Socket socket = new Socket(host, SERVER_PORT)) {
            Thread thread = new Thread(() -> {
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
                    String message;
                    while ((message = reader.readLine()) != null) {
                        System.out.println(message);
                        if (!logout.equals("") && message.equals("200 OK")) {
                            System.exit(0);
                            break;
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            thread.setDaemon(true);
            thread.start();

            PrintWriter writer = new PrintWriter(socket.getOutputStream());
            while (true) {
                System.out.println("1. LOGIN\n2. SOLVE\n3. LIST\n4. SHUTDOWN\n5. LOGOUT");
                switch (in.nextLine()) {
                    case "1":
                        System.out.println("USERNAME");
                        String userName = in.nextLine();
                        System.out.println("PASSWORD");
                        String password = in.nextLine();
                        writer.println("LOGIN " + userName + " " + password);
                        break;
                    case "2":
                        writer.println("SOLVE");
                        break;
                    case "3":
                        System.out.println("FLAGS(-all | <empty>)");
                        String flag = in.nextLine();
                        writer.println("LIST" + (flag.equals("-all") ? " -all" : ""));
                        break;
                    case "4":
                        writer.println("SHUTDOWN");
                        break;
                    case "5":
                        writer.println("LOGOUT");
                        logout = "LOGOUT";
                        break;
                }
                writer.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;

public class Server {
    public static final int SERVER_PORT = 2022;
    public static final String ROOT = "admin";
    static String name = "";


    public static void main(String[] args) {
        HashMap<String, String> accounts = new HashMap<>();
        accounts.put(ROOT, "admin");
        accounts.put("Tom", "Tom");
        while (true) {
            try (ServerSocket serverSocket = new ServerSocket(SERVER_PORT)) {
                Socket socket = serverSocket.accept();
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
                    PrintWriter writer = new PrintWriter(socket.getOutputStream());
                    String message;
                    while ((message = reader.readLine()) != null) {
                        if (message.startsWith("LOGIN")) {
                            String[] data = message.split(" ");
                            if (data.length == 3 && accounts.get(data[1]) != null && accounts.get(data[1]).equals(data[2])) {
                                writer.println("SUCCESS ");
                                name = data[1];
                            } else {
                                writer.println("FAILURE: Please provide correct username and password. Try again.");
                            }
                        } else if (message.equals("SOLVE")) {
                            writer.println("SOLVE");
                        } else if (message.startsWith("LIST")) {
                            if (!name.equals("")) {
                                String[] data = message.split(" ");
                                if (data.length > 1 && data[1].equals("-all") && name.equals(ROOT)) {
                                    writer.println("CONTENT OF ALL");
                                } else {
                                    writer.println("CONTENT OF " + name + ".txt");
                                }
                            } else {
                                writer.println("LIST");
                            }
                        } else if (message.equals("SHUTDOWN")) {
                            writer.println("200 OK");
                            writer.flush();
                            return;
                        } else if (message.equals("LOGOUT")) {
                            writer.println("200 OK");
                        }

                        writer.flush();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS