Answer to Question #294516 in Java | JSP | JSF for Ahsan wains

Question #294516

You will write two programs, a server and a client. The server creates a socket in the Internet domain the results to the requester. The client will also create a socket in the Internet domain, send requests to the SERVER_PORT of a computer specified on the command-line, and receive responses through this socket from a server. For this assignment, you just need to allow one active client to connect to the server. For this project, you will not allow new user sign-up or deletion of any of the users. bound to port SERVER_PORT (a constant you should define in both programs, you may use 4 digits of your birth year). The server receives requests through this socket, acts on those requests, and returns

Your client operates by sending LOGIN, SOLVE, LIST, SHUTDOWN, LOGOUT commands to the server. You should create a client that is able to send any of the commands above, and allows a user to specify which of the commands the client should send to the server. 


1
Expert's answer
2022-02-06T17:17:40-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;

    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);
                    }
                } catch (Exception e) {
                }
            });
            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");
                        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) {
        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("SHUTDOWN");
                            break;
                        case "LOGOUT":
                            writer.println("LOGOUT");
                            break;
                    }
                    writer.flush();
                }
            } catch (Exception e) {
            }
        } catch (Exception e) {
        }
    }
}

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