20-CS-694 Advanced Programming Techniques Spring 2012
Homework Assignment 4

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

Instant Messenger Server and Client

Due: May 10, 2012 (submit instructions: here)

Rationale:
    This is the prep assignment for the project. It involves many aspects of Networking that will be used later.
 
Homework Problem:
Write an instant messenger server and client. Specifications are as follows:
    Server:
  1. Listens on port 8081 for client requests to register. Expected message from a client is
          <handle> <port>\n
    
    where \n is the newline character. Stores registered clients in a hashtable. Hash key is the handle. A ClientObject consisting of handle, ipaddress, and port may be used as the stored object.

  2. Listens on port 8080 for messages to be forwarded to a buddy. Receives messages of the following type:
          <client> <buddy>\n<message-to-forward>\n
    
    The hashtable is checked to see whether the buddy is online. If not, the message bad\n is returned to the client. If so, the message is forwarded to the buddy and an ok\n message is sent to the client.

  3. Listens on port 8082 for client requests to see the list of registered clients. When a connection is established, the contents of the hashtable are converted to a String and sent back to the client. The form of the returned String is, for example:
          franco 24.10.45.90 9035|peanuts 55.43.231.220 8633
    
    which specifies the handle, machine ip, and port of each connected client, and uses "|" to separate records.

  4. Listens on port 8083 for requests from clients to unregister. Received message is:
         <client-handle>\n
    
    The hashtable is checked. If client is in the hashtable, it is removed. A status message is returned to the client.

  5. Once a second, checks whether there is still a connection to each client that is registered. In the event one is not responding, removes that client from the data base. In case the situation is different from a second ago, sends a simple message to each connected client to update their client list If no update is needed, just two newline characters are sent, otherwise a newline, followed by the character 'U' followed by newline is sent.
Client:

  1. Registers with the server using server port 8081. Chooses random client port to make further connections. Message sent to server consists of client-handle and port. If connection to server succeeds, an OnLineHandler is started which listens for server messages on chosen port. If the handler receives two newline characters in a row (two empty lines), it is assumed to be a server "ping" to check whether the client is connected: no action should be required by the client. If the handler receives two non-empty lines a buddy message is being received and is handled by displaying it in a TextArea. Otherwise the handler receives an empty line and a 'U' followed by newline and it is assumed this is a server command to inform this client that its buddy list needs to be updated.

  2. Capable of unregistering by sending a message containing client-handle to port 8083 of server.

  3. Maintains a list of registered clients (by handle) as a Choice box (or JComboBox). Updates the buddy list on signal from the server or by client user request by connecting to server port 8082 and reading the registered client list (see server requirements above). The form of the returned list is a String consisting of free form records of three fields: name, ipaddress, port. Records are separated by "|" and the '\n' character terminates the String. The records are parsed and inserted into a Choice box which is the buddy list.

  4. Sends a message to a buddy. The message is sent through the Server using port 8080. Server ip is taken from a "server" TextField. The message to the server consists of a header and the message to forward to the buddy. The header consists of the handle of the client, taken from a "handle" TextField, and the handle of the buddy, taken from the "buddy" Choice Box (or JComboBox). The header is terminated with '\n'. The forwarded message follows the handle. If "ok" is not returned by the server then it is assumed the buddy is not connected and an error message is generated and displayed.
 
Programming Notes:
Here is a sample client. Try it. Get a few buddies together and point to gauss.ececs.uc.edu (the default) and enter a unique handle. Then click on the online button. Everyone should then be selectable from the buddies menu. If you leave this page and reenter without restarting your webbrowser, things might be amiss.