CPSC 441, Networking and Distributed Processing, Fall 2004
Lab 3: Network Programming I
THE MAIN GOAL for the network programming part of the course is to implement a multi-threaded web server in Java. However, we will start with a simpler example: A simple chat client and server. You will be working for this lab on one of the computers in the Math/CS computer lab.
Start by testing the server. Open a console window and cd to the directory /classes/f04/cs441. This directory contains compiled class files ChatServer.class and ChatClient.class.
Start the ChatServer by giving the command "java ChatServer". The server will wait for a connection request from a ChatClient. Then, on another computer (or from another console window on the same computer), use the command "java ChatClient cslabX", where cslabX is the name of the computer where the server is running.
A connection will open where the two sides can take turns sending lines of text to each other. The server side has to go first. The conversation ends when one side or the other enters a blank line. It would also end if one of the programs is terminated unexpectedly. After the connection closes, the server will wait for another connection. The server can be terminated by typing a CONTROL-C in the console window where the server is running.
Your main assignment for this lab is to write Java programs that duplicate the behavior of ChatServer and ChatClient as closely as possible. You don't have to write the programs from scratch -- you can start from the GenericServer and GenericClient that we looked at in class.
Begin by copying the source code files GenericServer.java and GenericClient.java from the directory /classes/f04/cs441 into your own directory. You will want to rename the files. (Don't forget that the name of the class defined in the file must also be changed to match the name of the file.) Edit the renamed files to produce your own chat server and client.
Except for one small detail, all the code that you need to write goes into the handleConnection method. The code in the server and in the client can be very similar. They are not identical since the server must start by sending a message and the client must start by receiving a message.
(You will need a convenient way of reading a line of input from the user. The easiest way to do this is to make a BufferedReader to read from standard input:
BufferedReader userIn; userIn = new BufferedReader(new InputStreamReader(System.in));Then you can use userIn.readLine() to get the user's input.)What to turn in: You should turn in printouts of your server and client programs. This is an individual lab. Every person in the class should turn in their own programs. This can be turned in next Friday.
Starting Your Web Server: Next week in lab, you will be working on your web server. As a preparation for this, you should program a very simple Web server, along the lines of the Web server in Section 2.9 of the textbook except that it should be based on GenericServer.java. (Since it will be based on GenericServer, it will be able to handle more than one request, and it will handle exceptions in a reasonable way.)
You will be able to work on the Web server project in a group. Your group should have a complete Web server, or as close to it as you can come, before you arrive at lab next Friday. You can start work on this during today's lab if you have time.
Fun with Client/Server: Try playing a game of FooBilliards against an opponent over the network. You will have to figure out how to use the program as a server and as a client...