Answer to Question #294520 in Java | JSP | JSF for najam

Question #294520

SHUTDOWN The SHUTDOWN command, which is sent from the client to the server, is a single line message that allows a user to shutdown the server. A user that wants to shutdown the server should send the ASCII string "SHUTDOWN" followed by the newline character (i.e., '\n'). Upon receiving the SHUTDOWN command, the server should return the string "200 OK" (terminated with a newline), close all open sockets and files, and then terminate. A client-server interaction with the SHUTDOWN command looks like: c: SHUTDOWN s: 200 OK

LOGOUT Terminate only the client. The client exits when it receives the confirmation message from the server. A client-server interaction with the LOGOUT command looks like: C: LOGOUT S: 200 OK

Format You may work in a team of no more than two students. You may choose to work by yourself, or with one other person. Not 12 other people. Not 3 other people. Not 2 other people. Teams can be only 2 people.


1
Expert's answer
2022-02-06T17:17:32-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":
                        writer.println("LOGIN");
                        break;
                    case "2":
                        writer.println("SOLVE");
                        break;
                    case "3":
                        writer.println("LIST");
                        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;

public class Server {
    public static final int SERVER_PORT = 2022;

    public static void main(String[] args) {
        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) {
                        switch (message) {
                            case "LOGIN":
                                writer.println("LOGIN");
                                break;
                            case "SOLVE":
                                writer.println("SOLVE");
                                break;
                            case "LIST":
                                writer.println("LIST");
                                break;
                            case "SHUTDOWN":
                                writer.println("200 OK");
                                writer.flush();
                                return;
                            case "LOGOUT":
                                writer.println("200 OK");
                                break;
                        }
                        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