Question: In this assignment, you will implement a simple chat room using java. This will require a server program. The server is multi-threaded. It maintains a
In this assignment, you will implement a simple chat room using java. This will require a server program.
The server is multi-threaded. It maintains a list (a ArrayList will work well) of all the active connections. It will listen on a port for a new connection.
You have the authority to choose the port number and design appropriate protocol for client-server communication.
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 asking for an ID 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 references).
From that point on, anything that is typed by the client is relayed to all chat room occupants (including the client), with the 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).
You will need to be careful to synchronize access to the array list using locks, 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).
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
