CIT 597 Server/Client Notes
Fall 2005, David Matuszek

What the programs do

There seems to be some confusion about the current assignment. It is very important to understand that the client and the server are two completely separate programs. They could run on two separate computers, and talk to each other over the internet. (In fact, it is silly to run them both on the same computer, except for purposes of testing.)

Here is (roughly) the series of events:

Time User Client Server
    Start up
    Open a ServerSocket on some port
    Call accept() on the ServerSocket, and block waiting for input
  Start up (Server must already be running)  
  Open a Socket with a URL to the Server and the same port number that the Server is using
After this, the Client only does something in response to an Event (a user action)
Request a file to be read    
  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())
  Display the file to the user  
Request that the file be saved    
  save the file  

The Server does not shut down automatically. You can shut it down manually; this is what is usually done, and what I assumed when I wrote the assignment. You can do something fancier (and more unusual) if you like, but don't have the Server quit after each request.

Things I've learned

Mixing Swing components with AWT components (such as putting a Panel instead of a JPanel into a JFrame) can cause some rather interesting display bugs.

JEditorPane has a nice convenience method setContentType(mimeType) that handles the EditorKit stuff for you.

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. And while I'm on the subject, you should recognize the extensions .rtf, .htm, and .html--anything else, even binary, can be treated as plain text.

To avoid having the text in the JEditorPane centered and occupying a small area, put it in the BorderLayout.CENTER area of a JPanel, and scroll on that JPanel. (Sorry, the exact details aren't in front of me at the moment, but that's the general idea.)

The HTML Editor Kit really can't handle all of HTML. In particular, a complete HTML page, with an <html> header, a <head>, a <title>, and some JavaScript, won't display at all. I haven't explored this enough to know exactly what causes the problem. But in any case, there's a simple cure:

If/when the Client sees a <body> tag, it should discard everything it got up to that point, including the <body> tag itself. This is easy; check for line.contains("<BODY") and line.contains("<body"). (Notice that the search strings don't include ">".) You can make the simplifying assumption that the body tag occurs on a line by itself.

Thanks to the students who have brought me such interesting problems (you know who you are).