Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// This file contains material supporting section 3.7 of the textbook: // Object Oriented Software Engineering and is issued under the open-source // license found

image text in transcribed

// This file contains material supporting section 3.7 of the textbook: // "Object Oriented Software Engineering" and is issued under the open-source // license found at www.lloseng.com import java.io.*; import ocsf.server.*; /** * This class overrides some of the methods in the abstract * superclass in order to give more functionality to the server. * * @author Dr Timothy C. Lethbridge * @author Dr Robert Laganière * @author François Bélanger * @author Paul Holden * @version July 2000 */ public class EchoServer extends AbstractServer { //Class variables ************************************************* /** * The default port to listen on. */ final public static int DEFAULT_PORT = 5555; //Constructors **************************************************** /** * Constructs an instance of the echo server. * * @param port The port number to connect on. */ public EchoServer(int port) { super(port); } //Instance methods ************************************************ /** * This method handles any messages received from the client. * * @param msg The message received from the client. * @param client The connection from which the message originated. */ public void handleMessageFromClient (Object msg, ConnectionToClient client) { System.out.println("Message received: " + msg + " from " + client); this.sendToAllClients(msg); } /** * This method overrides the one in the superclass. Called * when the server starts listening for connections. */ protected void serverStarted() { System.out.println ("Server listening for connections on port " + getPort()); } /** * This method overrides the one in the superclass. Called * when the server stops listening for connections. */ protected void serverStopped() { System.out.println ("Server has stopped listening for connections."); } //Class methods *************************************************** /** * This method is responsible for the creation of * the server instance (there is no UI in this phase). * * @param args[0] The port number to listen on. Defaults to 5555 * if no argument is entered. */ public static void main(String[] args) { int port = 0; //Port to listen on try { port = Integer.parseInt(args[0]); //Get port from command line } catch(Throwable t) { port = DEFAULT_PORT; //Set port to 5555 } EchoServer sv = new EchoServer(port); try { sv.listen(); //Start listening for connections } catch (Exception ex) { System.out.println("ERROR - Could not listen for clients!"); } } } //End of EchoServer class 
In a similar manner to the way you implemented commands on the client side, add a mechanism so that the user of the server can type commands that perform special functions. 1. #quit cause the server to terminate gracefully. 2. #stop causes the server to stop listening for new clients. 3. #close causes the server not only to stop listening for new clients, but also to disconnect all existing clients. 4. #setport calls the setPort method in the server. Only allowed if the server is closed. 5. #start causes the server starts to listening for new clients. Only valid if the server is stopped. 6. #getport displays the current port number. In a similar manner to the way you implemented commands on the client side, add a mechanism so that the user of the server can type commands that perform special functions. 1. #quit cause the server to terminate gracefully. 2. #stop causes the server to stop listening for new clients. 3. #close causes the server not only to stop listening for new clients, but also to disconnect all existing clients. 4. #setport calls the setPort method in the server. Only allowed if the server is closed. 5. #start causes the server starts to listening for new clients. Only valid if the server is stopped. 6. #getport displays the current port number

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_2

Step: 3

blur-text-image_3

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

50 Tips And Tricks For MongoDB Developers Get The Most Out Of Your Database

Authors: Kristina Chodorow

1st Edition

1449304613, 978-1449304614

More Books

Students also viewed these Databases questions

Question

What is intel x86 architecture? And explain their organization

Answered: 1 week ago

Question

b. Why were these values considered important?

Answered: 1 week ago