In Java You are You are to modify ChatterServer and ChatterClient so that instea
ID: 3767264 • Letter: I
Question
In Java
You are You are to modify ChatterServer and ChatterClient so that instead of sending and reading one line at a time it allows the user to send and receive multiple lines. When it’s your turn you keep entering lines. When you are finished saying what you want you type “over” on a new line to allow the other guy to chat. When either guy is done they type “end” on a new line and both programs terminate.
————————————————————————————————
import java.io.*;
import java.net.*;
public class ChatterServer {
public static void main(String [] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(SERVER_PORT);
System.err.println("Waiting for a client");
Socket clientSocket = serverSocket.accept();
System.out.println("Connection requested from: " + clientSocket.getLocalAddress());
String myReply = "";
PrintWriter toClient = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader fromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
String endstr = "end";
toClient.println("Whatcha want?");
String incoming = fromClient.readLine();
while(incoming.compareTo(endstr) != 0) {
System.out.println(incoming);
System.out.print("Your turn> ");
myReply = keyboard.readLine();
toClient.println(myReply);
if (myReply.compareTo(endstr) == 0)
incoming = endstr;
else
incoming = fromClient.readLine();
}
}
final static int SERVER_PORT = 3333;
}
————————————————————————————————
import java.io.*;
import java.net.*;
public class ChatterClient {
public static void main(String [] args) throws Exception {
Socket serverSocket = new Socket(args[0], SERVER_PORT);
String myReply="";
PrintWriter toServer = new PrintWriter(serverSocket.getOutputStream(), true);
BufferedReader fromServer = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
String incoming = fromServer.readLine();
String endstr = "end";
while(incoming.compareTo(endstr) != 0) {
System.out.println(incoming);
System.out.print("Your turn> ");
myReply = keyboard.readLine();
toServer.println(myReply);
if (myReply.compareTo(endstr) == 0)
incoming = endstr;
else
incoming = fromServer.readLine();
}
}
final static int SERVER_PORT = 3333;
}
Explanation / Answer
Modified Program:
/**ChatterServer.java**/
import java.io.*;
import java.net.*;
public class ChatterServer {
public static void main(String [] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(SERVER_PORT);
System.err.println("Waiting for a client");
Socket clientSocket = serverSocket.accept();
System.out.println("Connection requested from: " + clientSocket.getLocalAddress());
String myReply = "";
PrintWriter toClient = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader fromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
String endstr = "end";
toClient.println("Whatcha want?");
String incoming = fromClient.readLine();
while(incoming.compareTo(endstr) != 0) {
System.out.println(incoming);
System.out.print("Your turn> ");
String msg="";
while(!msg.equals("over") && !msg.equals("end"))
{msg=keyboard.readLine();
if(!msg.equals("end"))
{
if(!msg.equals("over"))
myReply +=msg+" ";
}
else
{
myReply=msg;
}
}
toClient.println(myReply);
if (myReply.compareTo(endstr) == 0)
incoming = endstr;
else
{
incoming = fromClient.readLine();
}
}
}
final static int SERVER_PORT = 3333;
}
/**ChatterClient.java**/
import java.io.*;
import java.net.*;
public class ChatterClient {
public static void main(String [] args) throws Exception {
Socket serverSocket = new Socket(args[0], SERVER_PORT);
String myReply="";
PrintWriter toServer = new PrintWriter(serverSocket.getOutputStream(), true);
BufferedReader fromServer = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
String incoming = fromServer.readLine();
String endstr = "end";
while(incoming.compareTo(endstr) != 0) {
System.out.println(incoming);
System.out.print("Your turn> ");
String msg="";
while(!msg.equals("over") && !msg.equals("end"))
{
msg=keyboard.readLine();
if(!msg.equals("end"))
{
if(!msg.equals("over"))
{
myReply +=msg+" ";
}
}
else
{
myReply=msg;
}
}
toServer.println(myReply);
if (myReply.compareTo(endstr) == 0)
incoming = endstr;
else
incoming = fromServer.readLine();
}
}
final static int SERVER_PORT = 3333;
}
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.