Assignment 9: Chat Room Server

You will implement a simple chat room. This will require a server program. Alot of the code is similar to the demo.

The server is multithreaded. It maintains a list (a ArrayList will work well) of all the active connections. It will listen on a port for a new connection. When one comes in, it spawns a thread to handle the connection, and the main thread resumes listening. The spawned thread sends out a message for a "handle" name on the socket. When the client replies, the connection information is added to the master list of chat room occupants (so there is a shared ArrayList of PrintWriter references. From that point on, anything that is typed by the client is relayed to all chat room occupants (including the client), with an ID identifying the client. Of course if null is read from the client, or the special value LOGOUT then the client has disconnected and you should remove the connection from the shared ArrayList and then exit the thread gracefully (i.e. stop the infinite loop in the run method). Don't worry too much about error checking. However, you will need to be careful to synchronize access to the array list, since otherwise you might delete an entry while another thread is trying to broadcast to everyone, potentially resulting in an out-of-bounds ArrayList access or worse. You can test the server by starting it and connecting via several telnet windows. (You can turn the echo on for the telnet windows to see what you are typing.)

Broadcast a message when the client first joins the chatroom, and then broadcast a message when the client leaves the chatroom (in the finally block, after adjusting the ArrayList, but immediately prior to terminating the run method).