Docsity

Echo Lab: Network Programming with Java - Modifying an Echo Server, Lab Reports of Operating Systems

Toggle sidebar
bg1
Computer Systems 1 Echo Lab
CSC 2400 Computer Systems Fall 2008
Lab: EchoServer & EchoClient
Dr. Tom Way
About
An Echo server is a program that listens for incoming network messages and sends them
back to the client that sent them as a way to test the functioning of network programs. This
lab will give you some experience with network programming, in this case using Java by
running and then modifying an Echo server and client combination.
Setup
• Login to one of the Unix machines
• Make a directory called “echo” and change to that directory
• Copy files by typing: cp /mnt/a/tway/echo/* .
• Compile EchoServer.java and EchoClient.java by typing: make
Running
You will now run the EchoServer in one window while running the EchoClient in another.
To do this:
• Open a second window. For example, in SSH Secure Shell, on the menu select
Window->New Terminal.
• In the new window, change into the “echo” directory you created earlier.
• Run the server by typing: java EchoServer
• Run the client by typing what the server tells you to type.
• Experiment by typing in messages in the client and watching the output on both the
server and client windows
• When done, type “bye” in the client window and then hit Ctrl-C in the server window.
Modifications
Now, you will modify the EchoServer to be more useful.
• Edit the EchoServer.java program and add another condition in the handleInput method
to respond to the word “time” by returning the current time.
• Edit the EchoServer.java program and add another condition in the handleInput method
to respond to the word “date” by returning the current date.
• Edit the EchoServer.java program and add another condition in the handleInput method
to respond to the word “server” by returning information about the server.
• Add additional features as you think of them. You could try to answer questions, such
as “What time is it?” or “What is your name?” or other more sophisticate tasks.
• If you are feeling adventurous, you could add an “exit” command that causes the server
to exit completely if received from the client.
• Try connecting to a classmate’s server or the instructor’s server, or have them connect
to yours, to experiment with compatibility.
pf3

Partial preview of the text

Download Echo Lab: Network Programming with Java - Modifying an Echo Server and more Lab Reports Operating Systems in PDF only on Docsity!

CSC 2400 Computer Systems Fall 2008

Lab: EchoServer & EchoClient

Dr. Tom Way

About An Echo server is a program that listens for incoming network messages and sends them back to the client that sent them as a way to test the functioning of network programs. This lab will give you some experience with network programming, in this case using Java by running and then modifying an Echo server and client combination.

Setup

  • Login to one of the Unix machines
  • Make a directory called “ echo ” and change to that directory
  • Copy files by typing: cp /mnt/a/tway/echo/.*
  • Compile EchoServer.java and EchoClient.java by typing: make

Running You will now run the EchoServer in one window while running the EchoClient in another. To do this:

  • Open a second window. For example, in SSH Secure Shell, on the menu select Window->New Terminal.
  • In the new window, change into the “echo” directory you created earlier.
  • Run the server by typing: java EchoServer
  • Run the client by typing what the server tells you to type.
  • Experiment by typing in messages in the client and watching the output on both the server and client windows
  • When done, type “ bye ” in the client window and then hit Ctrl-C in the server window.

Modifications Now, you will modify the EchoServer to be more useful.

  • Edit the EchoServer.java program and add another condition in the handleInput method to respond to the word “time” by returning the current time.
  • Edit the EchoServer.java program and add another condition in the handleInput method to respond to the word “date” by returning the current date.
  • Edit the EchoServer.java program and add another condition in the handleInput method to respond to the word “server” by returning information about the server.
  • Add additional features as you think of them. You could try to answer questions, such as “What time is it?” or “What is your name?” or other more sophisticate tasks.
  • If you are feeling adventurous, you could add an “exit” command that causes the server to exit completely if received from the client.
  • Try connecting to a classmate’s server or the instructor’s server, or have them connect to yours, to experiment with compatibility.

handleInput method from EchoServer.java

// Handles input received from EchoClient, and returns // true if EchoServer can handle input, or false if it // cannot (typically meaning it should exit). public boolean handleInput(String line) throws IOException { String echoMsg; String printMsg = "Message received on port "+port+" was: " + line;

// Handle to case where the client says "bye" if ( (line == null) || line.equals("bye")) { System.out.println("Bye."); return false; } // Default case else { echoMsg = "Server heard port "+port+" say: " + line; }

// Write out a local message System.out.println(printMsg);

// Echo the message back to the client networkPout.write(echoMsg+"\n"); networkPout.flush();

return true; }