Networked VR Lab
Task 1 - Write a Threaded Echo Server (and Client) Application
Specification:
The server program will sit in a waiting state until a client logs on.
For each new client it will start a new thread that can handle communication
with that client.
Communication with the client involves:
sending a welcome message to the client (and specifying the
'logoff' command).
echoing any input (message strings) from the client.
Handling a 'logoff' message from the client.
The server should also be able to handle situations where the client application
terminates without sending the logoff message.
When starting the Server the command line argument will be the port the server
is listening on.
When starting the Client the command line arguments will be the server address
(IP address) and the port it is listening on.
Tips:
To find the IP Address of your machine type ipconfig into your Cygwin CLI.
(Or you can use localhost if the client application is connecting to the
same machine the server is running on).
When testing your applications you'll want to have them both running on the
same machine.
Start the server first, then the client.
You can do this by either starting your Server and backgrounding
it, then starting the Client(s) in the same shell window.
Or by opening different Cygwin windows for each application.
You can background an application by typing & after the
arguments to the application.
eg: java EchoServer 5777 &
Useful classes + methods:
ServerSocket
.accept()
Socket
.getInputStream()
.getOutputStream()
BufferedReader
PrintWriter
Thread
An easy way to start a new thread is to write a class that extends Thread.
This extending class must have the method
public void run()
To actually start this method running in a new thread call the start() method
of your extending class (start() automatically calls run()).
Common errors:
Remember to call the flush() method of your output stream to send the data
stored in its buffer (or turn autoflush on).
If you are using JOptionPane to allow the client to input messages to be
sent to the server remember that swing runs in a different thread so you
will have to call System.exit when your client application terminates otherwise
it will appear to hang.
Try to make your applications robust - theres no telling what data might
be sent to them - ie, Use lots of try-catch blocks.
Implementation Tip:
How does the server know when the client wishes to log off ?
Send a special string to the server.
A special string (or value of any kind) is called a sentinel value.
A sentinel value might be the word 'exit' for example. This lets the server
know that the client is about to close its connection.
Sentinel values can also be used to signal 'end of data entry'. For example
in this application only one line is echoed at a time, but if the user was
able to enter an arbitrary number of messages to send all at once we would
need to include a sentinel value at the end of the messages to indicate to
the server that the end of the input had been reached.
A problem can arise when you chose a sentinel value that is also a legitimate
data value - this can cause logic errors in your application. It can be difficult
to choose sentinel values if they are strings or characters as users can
enter anything. A good choice is a string that would be unlikely to be entered
in normal conditions.
<exit> or <logoff> might be good values.
Sample Output:
Server
|
Client 0
|
Client 1
|
>> java EchoServer 5777
Waiting for clients
Connection established with client 0
Client 0 : Hello
Connection established with client 1
Client 1 : Hello
Client 1 : Lets all move one place on
Client 0 : With sobs and tears he sorted out
Client 0 : Those of the largest size
Client 0 : I'll be going now
Client 1 : me too
client 0 logged off
Client 1 : bye
client 1 logged off
|
>> java EchoClient 127.0.0.1 5777
Server: Welcome to echo server.
Server: You are client 0
Server: type -exit to logoff
echo : Hello
echo : With sobs and tears he sorted out
echo : Those of the largest size
echo : I'll be going now
|
>> java EchoClient 127.0.0.1 5777
Server: Welcome to echo server.
Server: You are client 1
Server: type -exit to logoff
echo : Hello
echo : Lets all move one place on
echo : me too
echo : bye
|