CMSC 417-0101
|
PROJECT 1
|
September 13, 1999
|
Due: September 20, 10:00pm (via submit)
Introduction
This project introduces the use of threads and networking sockets in Java.
(It does not incorporate the automatic testing; that will come later.)
The project consists of two interacting application programs, a server
and a client:
-
The server creates a socket in the Internet domain bound to port SERVER_PORT
(a constant that is passed as a argument), accepts connections from clients,
and responds to queries from each client. For this project, a query is
an arbitrary string and the response is the string prefixed by a RESPONSE_PREFIX
(also a constant that is passed as a argument).
-
The client creates a TCP connection to the server, reads a query (string
line) from standard input, sends the query to the server, waits for
the response, and then repeats this with the next query. It exits if the
query is the sring "Bye".
You are provided two files: Server.java and Client.java.
-
The class Server (in file Server.java) creates the server socket, listens
on the server port, accepts a client connection, launches a thread called
ServerThread, and goes back to listening on the server port. ServerThread
handles messages from the accepted client; i.e., receives queries, sends
responses, and exits upon receiving the query "Bye",
-
The class Client (in file Client.java) launches a thread called ClientThread,
reads queries (strings) from standard input, passes them to ClientThread,
and exits upon encountering query "Bye". ClientThread handles the communication
with the server; i.e., send query to server and receive response from server.
The following figure represents the layout of the classes:
Assignment
You are required to implement the classes ServerThread and ClientThread
described above.
and supply the source in files and ClientThread.java respectively.
-
The class ServerThread
-
Extends Thread
-
Has a constructor ServerThread(Socket cSock, String respPrefix),
where cSock is the socket for talking to client and respPrefix
is the RESPONSE_PREFIX. This constructor is called by Server.
-
Has a method public void run(), which is its runnable method.
-
Source is supplied in file ServerThread.java.
-
The class ClientThread
-
Extends Thread
-
Has a constructor ClientThread(String[] args), where the first
element of args indicates the server host name and the second
element of args indicates the server port number. This constructor
is called by Client.
-
Has a method public void sendPacket(String buf), where buf
is the query to be sent to the server. This method is called by Client.
-
The runnable method public void run().
Command line
The command line syntax for the Server and Client is given below. The
server program takes two command-line arguments: the response_prefix and
the server port. The client program also takes two command-line arguments:
the name of the host where your server is running and the port number of
this host.
java Server <RESPONSE_PREFIX> <server_port>
java Client <server_hostname> <port>
Useful Classes
Server Socket http://www.javasoft.com/products/jdk/1.1/docs/api/java.net.ServerSocket.html
Socket
http://www.javasoft.com/products/jdk/1.1/docs/api/java.net.Server.html
Thread
http://www.javasoft.com/products/jdk/1.1/docs/api/java.lang.Thread.html
String
http://www.javasoft.com/products/jdk/1.1/docs/api/java.lang.String.html
InetAddress http://www.javasoft.com/products/jdk/1.1/docs/api/java.net.InetAddress.html
BufferedReader http://www.javasoft.com/products/jdk/1.1/docs/api/java.io.BufferedReader.html
PrintWriter
http://www.javasoft.com/products/jdk/1.1/docs/api/java.io.PrintWriter.html
Useful Readings
Custom Networking http://java.sun.com/docs/books/tutorial/networking/index.html
Notes
Use netstat(1) to see what sockets have been created by your programs
(you may have put them in the background; once a program terminates, the
sockets it created go away).