||Advanced Programming Techniques
||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)
This is the prep assignment for the project. It involves many aspects of
Networking that will be used later.
Write an instant messenger server and client. Specifications are as follows:
Listens on port 8081 for client requests to register. Expected message
from a client is
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.
Listens on port 8080 for messages to be forwarded to a buddy. Receives
messages of the following type:
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.
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 18.104.22.168 9035|peanuts 22.214.171.124 8633
which specifies the handle, machine ip, and port of each connected client,
and uses "|" to separate records.
Listens on port 8083 for requests from clients to unregister. Received
The hashtable is checked. If client is in the hashtable, it is removed.
A status message is returned to the client.
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.
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.
Capable of unregistering by sending a message containing client-handle to
port 8083 of server.
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.
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.
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.