CIT 597 Assignment 6: Simple file server and client
Fall 2007, David Matuszek

Purposes of this assignment:

General idea of the assignment:

Write a server program which, when requested for a file, sends that file. The server also keeps a log of all requests and responses.

Write a client program that can request files and save them.


Write a server to send files upon request, and a client to request them. These are two separate programs, intended to run on two different computers (although we will be running both on the same computer.) Both programs will be Java applications.

For a simple example of a client and a server, see and The server simply gets a text string from the client, reverses it, and sends it back. These examples are from Advanced Internet Applications with Java (2nd edition) by Art Gittleman.

ReverseClient expects an IP address as its single parameter to main; the parameter may be localhost to say "this computer".

The server will start listening on a specific port (use port 5000).

The client will initiate communications on that same port. For communication to occur, the server must start up before the client.

The GUI should have (at least) these elements:

You can use other GUI elements as you choose, so long as your interface is simple and obvious.

When the file has been received, the client should open a JFileChooser dialog to allow the user to save the file. The program should not quit after downloading a single file.

You do not need to write a multithreaded server.

To help you with Swing, download and unzip it in a directory of your choice. This will give you an executable Jar file, named SwingExamples.jar. You can run the program by double-clicking on the jar file--if you can't run it this way, you don't have Java properly installed. This program is meant to be run--you can look at the source code if you like, but you may find it pretty confusing.

Flow of control:

Here's what should typically happen:

Time User Client Server
    Start up
    Open a ServerSocket on port 5000
    Call accept() on the ServerSocket, and block waiting for input
  Start up (Server must already be running)  
Request a file (possibly a log file) to be read    
  Open a Socket with a URL to the Server and the same port number that the Server is using  
  Read in (from a JTextField) the name of a file from the user  
  Send the name of the file to the Server  
    The accept() call unblocks and returns a Socket
    Read from the Socket (null indicates nothing more to read)
    Read the file indicated in the message
    Write the file to the Socket and close it (because this transaction is done)
  Read the file from the Socket (null indicates nothing more to read) Return to step 3 (accept())
  Close the Socket  
  Display a JFileChooser dialog box so the user can choose where to save the file  
Choose a location to save the file (or Cancel) Save the file, and return to step 5.  


When this program is running, anyone can download files from your computer. Don't think that the use of an unusual port number provides any safety. It can be easily found by a port scanner.

Here are two required features of your file server:

However, for maximum security, avoid running your server while connected to the internet.


You really should set your Windows machines to display the file extensions for you (Tools : Folder Options : View : Hide extensions for known file types should be unchecked.) Otherwise, maybe the reason you can't read the file named foo.html is that it's really named foo.htm.

When finished, you should shut the server down manually. Anything else (having the Client shut it down, for example) is just weird.

Due date:

Wednesday, October 31, before midnight. Zip and turn in all relevant files via Blackboard.