Client - Server - Programs

Client-Server programs are the essential mechanisms that make the Internet what is is today. Almost all services offered on the Internet are based on this client-server model:

Ftp: the Ftp client requests a files and the Ftp server gives it to the client who in turn saves it on the local computer. Both speak according to the ftp protocol
Web: the Web client (e.g. Netscape) requests information (via a URL), the Web server gives it to the client, and the client interprets the information and displays it on the screen. Both speak according to the http protocol
Search Engines: you tell the search engine what you want to search for, the search engine does the work of finding it and delivers it to you (can you see who's the client and who the server ?). Both speak according to some protocol known to both

In this segment we want to write our own client-server programs. Just as with the TickerTape applet we dealt with previously we will proceed in stages:

Stage 1 - Echo Client: Most Unix machines run an echo server on port 7. That server accepts any client and simply echoes back to the client whatever the client tells the server. Here is a simple Echo Client, written in Java.
Note 1: The Echo Client is not and applet. You must download the source code, compile it on your machine, and then run it via the command 'java EchoClient'.
Note 2:The Echo Client was taken from the online Java tutorial by SUN and slighly modified.
Stage 2 - A Simple Joke Server: Next we want to write our own server program as well as the client program. Taking an idea from the online Java tutorial, we will use Knock-Knock jokes as an example. A typical client-server conversation would look like this:
Client: request connection with JokeServer
Server: Knock-Knock
Client: Who's there?
Server: Little Old Lady
Client: Little Old Lady Who?
Server: I didn't know you could yodel
Client: disconnects from JokeServer

This time, our program consists of several objects: the JokeClient object, a client program very similar to the previous Echo client, the JokeServer object, the basic server object providing the communications methods, and the JokeHandler object, the object implementing the protocol that client and server speak.

Note 1: Neither the client nor the server are applets. You must download the source code, compile it on your machine, and then run it via the command 'java JokeClient' (for the client) and 'java JokeServer' (for the server). And, in fact, this will not work. Take a look at the source code to see why.
Note 2:This code was taken from the online Java tutorial by SUN and slighly modified.
Stage 3 - An improved Joke Server: Now we want to improve the previous programs so that we can embed them into a web page. In other words, we must turn the standalone client from above into an applet, and change the protocol that they both speak somewhat to accomodate that change.

Here are the various pieces that make up the server: the JokeServer object, the basic server object providing the communications methods, and the JokeHandler object, the object implementing the protocol that client and server speak.

Here are the pieces that make up the client: the LineClient object, a generic client object that can be used for many client-server applications, the JokeClient object, the client specific to our situation, and the JokePlayer object, which plays a joke interatively inside the JokeClient applet.

The client is still a standalone program and needs to be compiled and run via the javac and java commands. However, since the client is an applet, you can actually see how it works by clicking here. The server has been installed previously on a Unix machine so that it can communicate with this client.

Stage 4 - the (almost) finished version: Before I'll show you the code, take a look at the client part yourself. The password for adding, modifying, or deleting jokes is JamesBond007 (notice the capital and small letters). Why don't you try to add your favorite knock-knock joke to the joke database, and, in the process, maybe find out what the one big mistake with that (almost) finished applet is.

Again, the server has been installed previously so that your client can connect to it. In fact, the server for this version understands more commands than the one for the previous version, but it is 'downward compatible' with the previous server. Thus, only one server runs and serves both the improved and the finished client. Try it by adding a joke in the finished version and seeing that joke in the improved version.

(bgw)