Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help me to design the code for this problem in Java. Use Sockets for comm

ID: 3712725 • Letter: P

Question

Please help me to design the code for this problem in Java.

Use Sockets for communication between processes.
Your task is to write a client and a server.

The client and server will demonstrate a message posting system. The server will maintain messages posted by clients, which clients can retrieve and view.

The client will:
1) Accept a machine name and port number to connect to as command line arguments.
2) Connect to the server.
3) Prompt for and send the user’s name.
4) Present the following menu of choices to the user:
a. Display the names of all known users.
b. Display the names of all currently connected users.
c. Send a text message to a particular user.
d. Send a text message to all currently connected users.
e. Send a text message to all known users.
f. Get my messages.
g. Exit.
5) Interact with the server to support the menu choices.
6) Ask the user for the next choice or exit.

The server will:
1) Accept a port number as a command line argument.
2) Accept connections from clients.
3) Create a new thread for each client.
4) Store messages sent to each user.
5) End by termination with control-C.

The server thread will:
1) Accept and process requests from the client.
2) Add the user’s name to the list of known users.
3) Provide mutual exclusion protection for the data structure that stores the messages.
4) Send only the minimal data needed to the client, not the menu or other UI text.
Other rules:

1) Each client transaction should interact with the server. Clients will not communicate directly with each other.
2) Configuration: your server should support multiple different clients at the same time, but should not allow the same user name to have more than one connection at the same time.
3) Authentication: assume the client has privileges to use the system—do not require a password.
4) Limits: you can assume a maximum of 100 known users, and a maximum of 10 messages each, where each message is at most 80 characters long.
5) Persistence: when the server exits the messages it is storing are lost. They will not be saved to a file. When a user gets their messages, those messages are removed from the server.
6) Users: a known user is any user who has connected during the server session, but may or may not be currently connected. Also, a message sent to an unknown user makes them known.
7) Errors: obvious errors should be caught and reported. For example, an invalid menu choice.
8) Output: your output should use the same wording and format as the sample output.

Sample output (assumes Joe has already connected):

Client output for user Sue:

>client cs1 2005

Connecting to cs1:2005

Enter your name: Sue

1. Display the names of all known users.
2. Display the names of all currently connected users.
3. Send a text message to a particular user.
4. Send a text message to all currently connected users.
5. Send a text message to all known users.
6. Get my messages.
7. Exit.
Enter your choice: 1

Known users:
1. Sue
2. Joe

1. Display the names of all known users.
2. Display the names of all currently connected users.
3. Send a text message to a particular user.
4. Send a text message to all currently connected users.
5. Send a text message to all known users.
6. Get my messages.
7. Exit.
Enter your choice: 3

Enter recipient's name: Joe
Enter a message: Hello Joe

Message posted to Joe

?
1. Display the names of all known users.
2. Display the names of all currently connected users.
3. Send a text message to a particular user.
4. Send a text message to all currently connected users.
5. Send a text message to all known users.
6. Get my messages.
7. Exit.
Enter your choice: 6

Your messages:
1. From Joe, 04/11/13 03:14 PM, Hello Sue
2. From Joe, 04/11/13 03:15 PM, What’s new?

1. Display the names of all known users.
2. Display the names of all currently connected users.
3. Send a text message to a particular user.
4. Send a text message to all currently connected users.
5. Send a text message to all known users.
6. Get my messages.
7. Exit.
Enter your choice: 7

Server sample output:

>server 2005


04/11/13, 3:10 PM, Connection by known user Sue.
04/11/13, 3:12 PM, Connection by unknown user Joe.
04/11/13, 3:13 PM, Sue displays all known users.
04/11/13, 3:13 PM, Sue posts a message for Joe.
04/11/13, 3:13 PM, Joe displays all connected users.
04/11/13, 3:14 PM, Joe posts a message for Sue.
04/11/13, 3:15 PM, Joe posts a message for Sue.
04/11/13, 3:17 PM, Sue gets messages.
04/11/13, 3:20 PM, Sue exits
04/11/13, 3:21 PM, Joe exits

Explanation / Answer

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class SocketClient {

   /**
   * The client will:
       1)   Accept a machine name and port number to connect to as command line arguments.
       2)   Connect to the server.
       3)   Prompt for and send the user’s name.
       4)   Present the following menu of choices to the user:
           a.   Display the names of all known users.
           b.   Display the names of all currently connected users.
           c.   Send a text message to a particular user.
           d.   Send a text message to all currently connected users.
           e.   Send a text message to all known users.
           f.   Get my messages.
           g.   Exit.
       5)   Interact with the server to support the menu choices.
       6)   Ask the user for the next choice or exit.
   */
  
   Socket socket = null;
   PrintWriter out = null;
   BufferedReader in = null;
  
   static String newLine = System.getProperty("line.separator");
   String menu = newLine +
           "Select from the following menu:" + newLine +
                       "       1.   Display the names of all known users." + newLine +
                       "       2.   Display the names of all currently connected users." + newLine +
                       "       3.   Send a text message to a particular user." + newLine +
                       "       4.   Send a text message to all currently connected users." + newLine +
                       "       5.   Send a text message to all known users." + newLine +
                       "       6.   Get my messages." + newLine +
                       "       7.   Exit." + newLine + newLine +
                       "Enter your choice: ";
  
   public static void main(String[] args)
   {
       if (args.length != 2) {
           System.out.println("Usage: client hostname port");
             System.exit(1);
          }

         SocketClient client = new SocketClient();

         String host = args[0];
         int port = Integer.valueOf(args[1]);
         client.listenSocket(host, port);
         client.communicate();
       
       }

   private void communicate() {
       Scanner sc = new Scanner(System.in);
        System.out.println("Enter your name: ");
        String name = sc.nextLine();

        //Send data over socket
        out.println(name);

        //Receive text from server
        try {
           String line = in.readLine();
            System.out.println("Text received: " + line);
        }
        catch (IOException e) {
            System.out.println("Read failed");
            System.exit(1);
        }
        String option = "";
        while (!option.equals("7")) {
           System.out.print(menu);
     
           option = sc.nextLine();
           // if option not in 1-7, error, else...
          
           if (!option.matches("[1-7]")) {
               System.out.println(" " + option + " is not a valid option" + newLine);
               continue;
           }
           //Send data over socket
            out.println(option);
      
           //Receive text from server
            String line = "";
           try {
               line = in.readLine();
                // System.out.println("Text received: " + line);
            }
            catch (IOException e) {
                System.out.println("Read failed");
                System.exit(1);
            }
           // process line according to option sent...
           switch (option) {
           case "1":       // 1.   Display the names of all known users.
               System.out.println("Known users:");
               String[] allUsers = line.split(",");
               int count = 0;
               for (String user: allUsers) {
                   count++;
                   System.out.println("   " + count + "   " + user);
               }
               break;
           case "2":
                System.out.println("Currently connected users:");
               String[] connectedUsers = line.split(",");
               int count1 = 0;
               for (String user: connectedUsers) {
                   count1++;
                   System.out.println("   " + count1 + "   " + user);
               }
               break;
           case "3":
               System.out.println("3. Send a text message to a particular user.");
               break;
           case "4":
               System.out.println("4. Send a text message to all currently connected users.");
              
               break;
           case "5":
               System.out.println("5. Send a text message to all known users.");
              
               break;
           case "6":
               System.out.println("6. Get my messages.");
               break;
           case "7":
               System.out.println("7. Exit.");
               break;
           default:
               System.out.println(newLine + "unable to process option " + option + newLine);
           }
          
          
        }
        System.out.println("Client exiting ...");
   }

   private void listenSocket(String host, int port) {
       //Create socket connection
          try {
              socket = new Socket(host, port);
              out = new PrintWriter(socket.getOutputStream(), true);
              in = new BufferedReader
                   (new InputStreamReader(socket.getInputStream()));
          }
          catch (UnknownHostException e) {
              System.out.println("Unknown host");
              System.exit(1);
          }
          catch (IOException e) {
              System.out.println(e.getMessage());
              System.out.println("No I/O");
              System.exit(1);
          }
   }
}