| CIT
597 Assignment 6: Simple file server and client Fall 2007, David Matuszek |
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 ReverseClient.java and ReverseServer.java. 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.
ReverseClientexpects an IP address as its single parameter tomain; the parameter may belocalhostto 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 SwingExamples.zip
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.
Here's what should typically happen:
| Time | User | Client | Server |
|---|---|---|---|
1 |
Start up | ||
2 |
Open a ServerSocket on port 5000 |
||
3 |
Call accept() on the ServerSocket, and
block waiting for input |
||
4 |
Start up (Server must already be running) | ||
5 |
Request a file (possibly a log file) to be read | ||
6 |
Open a Socket with a URL to the Server and the same
port number that the Server is using |
||
7 |
Read in (from a JTextField) the name of a file from
the user |
||
8 |
Send the name of the file to the Server | ||
9 |
The accept() call unblocks and returns a Socket |
||
10 |
Read from the Socket (null indicates nothing
more to read) |
||
11 |
Read the file indicated in the message | ||
12 |
Write the file to the Socket and close it (because this
transaction is done) |
||
13 |
Read the file from the Socket (null indicates nothing
more to read) |
Return to step 3 (accept()) |
|
14 |
Close the Socket | ||
15 |
Display a JFileChooser dialog box so the user can choose
where to save the file |
||
16 |
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:
/ or \), everything
up to those slashes should be removed. If the name contains the sequence
"..", the request should be ignored (but logged). 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.