Ask Question, Ask an Expert


Ask Programming Language Expert

This assignment states you to prepare a multi-threaded chat-room server. The first practical assignment asked you to design and implement some data structures which would be helpful for this task; for this assignment, you will prepare the code which will implement a working chat-room server. Most of this code comprises setting up sockets to handle communication between the chat-room and the logged-in users and by using the input and output streams of such sockets to send and receive messages. Such messages should be in a specific format, so that client software can present the messages in a meaningful way. The format of the messages is describeed in the chat-room protocol below.

Chat-room Server:

A chat-room server permits remote users to connect across a network to the chat-room. Once they are connected, they can send messages to the chat-room: any message sent to the chat-room is broadcast to all connected users. Additionally, the chat-room server broadcasts notifications when a new chatter connects to the chat-room, and when an existing chatter leaves the chat-room. The chat-room server uses the given protocol for input/output:

A) Whenever a remote client connects to the server, the client must send a single, non-empty line of text that will be the name of the remote user in the chat-room. The server will then broadcast to all chatters in the room a string of the form " 0 " +   n, where n  is the name of chatter who has just joined.

B) The remote client has now "entered" the chat-room, and can send messages to the chat-room. The client can send two types of message:

i) A message to be broadcast: this might comprise of several lines. Each line must start with the prefix "2"; the end of the message is pointed out by a line consisting of just "3" (For illustration, to send a message "Hi\ neveryone", the remote client must send three lines:
" 2 H i \n2everyone \n 3\n"). The server then broadcasts one line of text of the form "2" + n , where n  is the name of the chatter who has sent the message; the server then sends all the lines of the message text (each line starting with " 2 ") and finally, a line comprising of just the text "3".

ii) A logout notification: this comprises of a single line starting with "1"; this points out that the remote client is leaving the chat-room and their session ends. The server broadcasts a string of the form "1" + n, where n is the name of the chatter who has just left.

If the remote client sends any input that does not conform to this protocol, the server ends the session.

One way to implement the functionality of the chat-room server would be to use the given classes:

A) Chatter - an instance of this class represents a single logged-in remote client. Each time a client connects to the server, an instance of Chatter is created; this instance will handle all interactions with that user. The class must listen for input from the client, and use the suitable ChatterList methods to broadcast the messages. In more detail, class Chatter must implement interface Runnable; when a client connects to the server, a Chatter instance must be used to handle all the communication between the server and that remote client; this Chatter will run in its own Thread.

B) ChatterList - this maintains a list of Chatter instances that represent the currently connected remote clients, and sends messages to all logged-in users, as in the 1st assignment. ChatroomServer - the top-level class which implements a server; this class has a main method that sets up a ChatterList to maintain a list of Chatter instances and then sets up a server socket which accepts incoming connections. Whenever a connection comes in from a remote client, an instance of Chatter is created that will handle all interactions with the client.

In addition, you must provide some means of shutting down the server. When the server is shut down, all Sockets and I/O streams must be closed, and all Threads should be ended.

The Chatter class would need several modifications to the class written for the 1st practical assignment:

A) Instances would require to store (in a field) the Socket instance which represents the connection to the remote client.

Instances would require running in a separate thread of computation (as a "session-handler" class). The class must therefore implement Runnable. The run () method must:

1) Set up the input and output streams from the Socket; then

2) Read a single line of input from the remote client: this will be the chatter's name; then

3) Add itself to the chat-room's list of chatters (see method connect (Chatter) from the first assignment; the Chatter will therefore require a reference to the ChatterList instance created in the main method of class ChatroomServer); then

4) Repeatedly read input from the remote client, and take the appropriate action:

i) If the input is a message to be sent to all users of the chat-room, the ChatterList instance should broadcast the message to all chatters (see method broadcast (String) from the first assignment);

ii) If the input is a logout notification, then end the session (see method l e a v e (Chatter) from the first assignment) and close the connection to the remote client.

B) The sendToUser (String) method must be implemented so that the given string is sent to the remote client, by using the output stream from the Socket.

Implementing the ChatroomServer class and making the above modifications to the Chatter class would give a working chat-room server. Though, the result would be instead inefficient. Every time a remote client connects to the server, a new Thread instance is made to handle the session with the remote client; when the remote client disconnects from the client, the Thread dies, however still exists in the JVM's memory. If the server has a lot of traffic, the JVM's memory will soon be cluttered with unused, dead threads. One solution would be to use method newCachedThreadPool ( ) in class java. util. concurrent . Executors, which gives an extensible pool of reusable threads.

For the same reason, it would be desirable to reduce the number of instances of other classes created by the server. Recall that the linked list in class ChatterList uses the class Node. When Chatter is added to the list, a new Node instance is created to store the Chatter. As we're trying not to clutter the JVM's memory with unused objects, care should be taken not to create unnecessary nodes. One solution would be to "merge" the Node and Chatter classes; to be more precise, move the "pointers" to the Chatter class and not use the node class at all. Chatters then have a field that stores the next Chatter in the list; you'll probably need a setTail(Chatter) method.

Likewise, to reduce the number of Chatter instances created, we might have the server maintain a list of "dead" but re-usable chatters (just as ChatterList maintains a list of "live" chatters). When a remote client connects to the chat-room, a dead chatter will be taken from this list (if the list is empty, a new Chatter should be created), and "initialized": this means that class Chatter needs a new method, initialize (Socket), that will instantiate the Socket field to the given parameter; once initialized, the Chatter session is run in a thread, as before.

One way of achieving this would be to modify the ChatterList class, so that it stores two lists: a linked list of live Chatters, as before; and a list of dead Chatters. It would also require a method, getChatter ( ), to return the first Chatter in the dead Chatter list (or a new Chatter instance if the dead Chatter list.

One further issue to be addressed is thread-safety. While the server is running, there will be one Chatter thread for each user in the chat-room; all of these threads call methods from one ChatterList instance. You should make sure that interference cannot arise in your solution.


The following is a suggested plan for completing this assignment (you do not need to follow this plan):

1) Modify the Chatter class so it stores a pointer to the next Chatter in whichever list it finds itself (null if there is no next Chatter).

2) Modify the ChatterList class so it uses class Chatter instead of class Node, and maintains two lists: a live Chatter list, and a dead Chatter list (you can either use your own solution to the first assignment, or use the model solution). Add method getChatter(), and modify the leave(Chatter) method so that the removed Chatter is added to the dead Chatter list.

3) Implement the server functionality in class ChatroomServer.

4) Make the other necessary changes to class Chatter.

5) Make sure your solution is thread-safe.

6) Make sure your solution is fully documented, and that your comments describe why your solution is thread-safe.

7) Submit your solution using the departmental submission system. You should submit all your Java source-code files. Typically, this might be,, and, but you might structure your classes in different ways, for ex by using inner classes.


Setting up a server socket can compromise network security. If you test your solution on the departmental network, you MUST:

i) Only run your server program under Linux (e.g., in an Exceed session);
ii) Only set up your server on your allocated port number.

The departmental technical support team has allocated a port number to each student taking this module; the list of allocated port numbers is available here. You should set up your server socket on host "localhost" at the port number you have been assigned.

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M9422

Have any Question? 

Related Questions in Programming Language

Lab 1a box of cookies can hold 24 cookies and a container

Lab 1 A box of cookies can hold 24 cookies, and a container can hold 75 boxes of cookies. Write a program that prompts the user to enter the total number of cookies, the number of cookies in a box, and the number of cook ...

Write a program where you create a class called bankaccount

Write a program where you create a class called BankAccount. The BankAccount class should contain a String to store the customer name and a double to store the account balance. The BankAccount class should have two const ...

Complete the below two problems each problem is a separate

Complete the below two problems. Each problem is a separate program (project). Question 1 Write a program that calculates and prints the monthly paycheck for an employee. The net pay is calculated after taking the follow ...

Asignmentbe sure to follow the coding standard guidelines

Assignment Be sure to follow the Coding Standard Guidelines. You must properly indent and comment your code. Indent code and insert comments to document your program. Program must be implemented and run as instructed. So ...

Programming project assessmentinstructions the following

Programming Project Assessment Instructions: The following programming problem can be solved by a program that uses three basic tasks-Input Data, Process Data, and Output Results. To process the data, use file, looping, ...

Pseudocodes and flowchartxyz company employees work for 8

Pseudocodes and flowchart XYZ company employees work for 8 hours a day and 40 hours in a week. The employees are paid overtime at the rate of half of the salary. Employees who get more than 15000 must pay a supplementary ...

Assignmentmany applications impose rules for user passwords

Assignment Many applications impose rules for user passwords. Write a program that utilizes methods to check a user entered password ***** print out either "Valid" or "Invalid", based on the following rules: • Password * ...

Creating methodssince you have created the data objects for

Creating Methods Since you have created the data objects for Harbor View Mooring, you now need to create processes associated with each object. In order to keep with good object-oriented practices of implementation hidin ...

Program 1 write a program that asks user to enter a year

Program 1: write a program that asks user to enter a year and this program checks whether that year is leap year or not. Also it should print out all a calendar for the year entered. Starting on Monday Jan 1st, 1900. Cha ...

Question 1a class is like a blueprint which you use to

Question 1 A class is like a blueprint which you use to create objects. An object is an instance of a class. It's a thing that you made out of a speci?c class. Basically, object and instance mean the same, but the word i ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Section onea in an atwood machine suppose two objects of

SECTION ONE (a) In an Atwood Machine, suppose two objects of unequal mass are hung vertically over a frictionless

Part 1you work in hr for a company that operates a factory

Part 1: You work in HR for a company that operates a factory manufacturing fiberglass. There are several hundred empl

Details on advanced accounting paperthis paper is intended

DETAILS ON ADVANCED ACCOUNTING PAPER This paper is intended for students to apply the theoretical knowledge around ac

Create a provider database and related reports and queries

Create a provider database and related reports and queries to capture contact information for potential PC component pro

Describe what you learned about the impact of economic

Describe what you learned about the impact of economic, social, and demographic trends affecting the US labor environmen