4: Simple file server and client
Fall 2005, David Matuszek
Write a server program which, when requested for a file, sends that file.
Write a client program that can request files and save them.
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 to
main; the parameter may be
localhostto say "this computer".
The server will start listening on a specific port (use port
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:
X, and the requested file is
foo/bar.html, then the server will look for it in
javax.swing.JEditorPaneto display the received file (or error messages, if necessary). Depending on the file received, display it as plain text, HTML, or RTF. You can find out how to do this by Sun's online tutorial, which is also referenced from the API.
JFileChooserdialog, so that you can save the file.
You can use other GUI elements as you choose, so long as your interface is simple and obvious.
The program should not quit after downloading a single file. You can have a Quit button if you like, or you can quit the program by simply closing the window.
You do not need to write a multithreaded server.
If you have only used the AWT, not Swing, I have a program and some examples
that should greatly ease the transition for you. Download SwingExamples.zip
and unzip it in a directory of your choice. This will unzip into two files,
SwingExamples.java. You must
keep these files together in the same directory. Run the program by double-clicking
As described, this very simple file server has no security features whatsoever. When it is running, anyone can download any file 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.
For some very limited security, the client should send some kind of password, which the server can check, and ignore any request that does not contain this password. The password can be hard-wired into both programs; the user does not need to know that it is there.
However, for maximum security, avoid running your server while connected to the internet.