Modify yourdocumentedKnockKnock program (single server version) sothat the client and server take turns exchanging String messages (Simple Instant Messenger) until someone says Bye, i.e. client or...


Modify yourdocumentedKnockKnock program (single server version) sothat the client and server take turns exchanging String messages (Simple Instant Messenger) until someone says Bye, i.e. client or server can end the conversation by saying Bye.Display a prompt to indicate whose turn it is to type a message. See whiteboard screen on Moodle for sample output of your program.

import java.net.*;import java.io.*;import java.util.Scanner;public class IMProtocol {private static final int WAITING = 0;private static final int INCONVERSATION = 1;private int state = WAITING;BufferedReader stdIn =new BufferedReader (new InputStreamReader(System.in));public String processInput(String theInput){ String theOutput = null; if(theInput != null){ state = WAITING; } if (state == WAITING){ theOutput = "Connection Establish "; state = INCONVERSATION; } else if (state == INCONVERSATION) { if(theInput == "Bye"){ theOutput = "Bye"; } else{ theOutput = theInput; } } return theOutput; }}import java.net.*;import java.io.*;
public class KnockKnockServer { public static void main(String[] args) throws IOException { if (args.length != 1) { System.err.println("Usage: java KnockKnockServer "); System.exit(1); }

int portNumber = Integer.parseInt(args[0]);

try ( ServerSocket serverSocket = new ServerSocket(portNumber); Socket clientSocket = serverSocket.accept(); PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(clientSocket.getInputStream())); ) { String inputLine, outputLine; // Initiate conversation with client IMProtocol kkp = new IMProtocol(); outputLine = kkp.processInput(null); out.println(outputLine);

while ((inputLine = in.readLine()) != null) { outputLine = kkp.processInput(inputLine); out.println(outputLine); if (outputLine.equals("Bye.")) break; } } catch (IOException e) { System.out.println("Exception caught when trying to listen on port " + portNumber + " or listening for a connection"); System.out.println(e.getMessage()); } }}import java.io.*;import java.net.*;
public class KnockKnockClient { public static void main(String[] args) throws IOException { if (args.length != 2) { System.err.println( "Usage: java EchoClient "); System.exit(1); }

String hostName = args[0]; int portNumber = Integer.parseInt(args[1]);

try ( Socket kkSocket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(kkSocket.getInputStream())); ) { BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); String fromServer; String fromUser;

while ((fromServer = in.readLine()) != null) { System.out.println("Server: " + fromServer); if (fromServer.equals("Bye.")) break; fromUser = stdIn.readLine(); if (fromUser != null) { System.out.println("Client: " + fromUser); out.println(fromUser); } } } catch (UnknownHostException e) { System.err.println("Don't know about host " + hostName); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection to " + hostName); System.exit(1); } }}
Mar 01, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here