Modify MessageClient so that it does something meaningful. Design an Application
ID: 3696006 • Letter: M
Question
Modify MessageClient so that it does something meaningful.
Design an Application Protocol
You must have at least 3 client request commands besides QUIT. You may add as many as you need.
Modify MessageClient so that it repeats until the user puts in the menu item to QUIT.
Modify MessageServer so that it continues to read from the inputStream and returns the appropriate responses to the menu in the MessageClient based on your Application Protocol, until it receives the QUIT request from the client.
Upload your modified MessageClient.java, MessageServer.java and your Application Protocol (in a .doc or .docx file).
/***************************************************************************************************************************************************************/
MessageClient.java
/*********************************************************************************************************************************************************************/
MessageServer.java
Explanation / Answer
MessageClient.java
package com.chegg.arrays;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class MessageClient {
public static void main(String args[]) {
Scanner scan = null;
try {
scan = new Scanner(System.in);
int option = 0;
do {
Socket client = new Socket("localhost", 8800);
InputStream instream = client.getInputStream();
OutputStream outstream = client.getOutputStream();
PrintWriter out = new PrintWriter(outstream);
Scanner in = new Scanner(instream);
System.out
.println("1. First 2. Second 3. Third 4. Quit Which option? ");
option = scan.nextInt();
out.print("" + option + " ");
out.flush();
String response = in.nextLine();
System.out.println(response);
in.close();
client.close();
} while (option != 4);
scan.close();
}
catch (UnknownHostException e) {
System.err.println("No such host");
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
}
MessageServer.java
package com.chegg.arrays;
import java.io.*;
import java.net.*;
import java.util.Date;
import java.util.Scanner;
class MessageServer {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(8800);
System.out.println("Waiting for clients to connect . . .");
while (true) {
Socket client = server.accept();
System.out.println("Client connected.");
Scanner in = new Scanner(client.getInputStream());
PrintWriter out = new PrintWriter(client.getOutputStream());
String whichMessageString = in.nextLine();
int whichMessage = Integer.parseInt(whichMessageString);
System.out.println("whichMessage " + whichMessage);
switch(whichMessage)
{
case 1:out.println("You have chosen the first option");
out.flush();
break;
case 2: out.println("You have chosen the second option");
out.flush();
break;
case 3 : out.println("You have chosen the third option");
out.flush();
break;
case 4 : out.println("");
out.flush();
break;
default : out.println("Invalid choice");
out.flush();
}
out.flush();
out.close();
client.close();
}
}
catch (IOException e) {
System.err.println(e.getMessage());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.