Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help with this lab exercise in Java a) Try out the multithreaded version of KnockKnock program, by creating a new project with classes KKMultiServer

Please help with this lab exercise in Java

a) Try out the multithreaded version of KnockKnock program, by creating a new project with classes KKMultiServer and KKMultiServerThread replacing KnockKnockServer (the other classes, KnockKnockClient and KnockKnockProtocol are the same as in the original example).

b) Modify it so that each server thread maintains an IM conversation with one client (as above). Allow the client to provide his/her name (as a command-line argument or read in from the keyboard) and include this as part of the message sent to the server, so that the server side will display both who sent the message and the message itself (e.g. "John: Hello").

There are the code mentioned above.

KKMultiServer:

import java.net.*;

import java.io.*;

public class KKMultiServer {

public static void main(String[] args) throws IOException {

if (args.length != 1) {

System.err.println("Usage: java KKMultiServer ");

System.exit(1);

}

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

boolean listening = true;

try (ServerSocket serverSocket = new ServerSocket(portNumber)) {

while (listening) {

new KKMultiServerThread(serverSocket.accept()).start();

}

} catch (IOException e) {

System.err.println("Could not listen on port " + portNumber);

System.exit(-1);

}

}

}

KKMultiServerThread:

import java.net.*;

import java.io.*;

public class KKMultiServerThread extends Thread {

private Socket socket = null;

public KKMultiServerThread(Socket socket) {

super("KKMultiServerThread");

this.socket = socket;

}

public void run() {

try (

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

BufferedReader in = new BufferedReader(

new InputStreamReader(

socket.getInputStream()));

) {

String inputLine, outputLine;

KnockKnockProtocol kkp = new KnockKnockProtocol();

outputLine = kkp.processInput(null);

out.println(outputLine);

while ((inputLine = in.readLine()) != null) {

outputLine = kkp.processInput(inputLine);

out.println(outputLine);

if (outputLine.equals("Bye"))

break;

}

socket.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

command-line argument:

//******************************************************************** // NameTag.java Author: Lewis/Loftus // // Demonstrates the use of command line arguments. //******************************************************************** public class NameTag { //----------------------------------------------------------------- // Prints a simple name tag using a greeting and a name that is // specified by the user. //----------------------------------------------------------------- public static void main (String[] args) { System.out.println (); System.out.println (" " + args[0]); System.out.println ("My name is " + args[1]); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions