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
1
    Start up
2
    Open a ServerSocket on some port
3
    Call accept() on the ServerSocket, and block waiting for input
4
  Start up (Server must already be running)  
5
  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)
 
6
Request a file to be read    
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
  Display the file to the user  
15
Request that the file be saved    
16
  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).