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

1 solve a simple payroll calculation find amount of pay

1. Solve a simple payroll calculation. Find amount of pay given hours worked and hourly rate. (pay = hourly rate * hours worked.) Run this values (hours = 30 and rate = 8.52) and with (hours = 53 and rate = 11.54). Show ...

Create a base class that will include the properties of

Create a base class that will include the properties of some type of data related to the company you work for or a favorite hobby. For example, I work for a healthcare provider, so my base class might be "Medical Facilit ...

Programming oneusing jgrasp and the software development

Programming One Using jGrasp and the Software Development Kit, write a program in response to the following prompt: Write a program that prompts the user to input three numbers. This program should then output the number ...

Write an mdi project that is a simple text editor allow the

Write an MDI project that is a simple text editor. Allow the user to open multiple documents, each in a separate child form. For the text editor, use one big textbox control with its multiline property set to True or a R ...

Handling exceptions in the guestbook applicationgeneral

Handling Exceptions in the Guestbook Application General guideline In this project, you will need to implement exception handling mechanism in the Guestbook application created in Lab. The code I provide to you does not ...

Spreadsheet exercise1 run the spreadsheet program in your

Spreadsheet Exercise 1. Run the spreadsheet program in your computer and type the following:   A B C D E F 1 PAYROLL 2 XYZ COMPANY Employee Name Department Degree Salary Need Increase? 3 Nasser Mechanical Master 15000   ...

Sum of digits in a string design a program that asks the

Sum of Digits in a String Design a program that asks the user to enter a string containing a series of single digit numbers with nothing seperating them. The program should display the sum of all the single digit numbers ...

1 devise a register-allocation strategy on the assumption

1. Devise a register-allocation strategy on the assumption that we automatically store all registers on the stack before each procedure call and restore them after the return. 2. Construct the register-interference graph ...

Project on grammarsnbspcourseist 230cmpsc

PROJECT ON GRAMMARS   Course: IST 230/CMPSC 360   Deadline: see the calendar in Canvas for the deadline   Objective: To acquire a comprehensive understanding of the application of grammars and formal language theory to c ...

Create a very basic calculator map out the numeric keypad

Create a very basic calculator, map out the numeric keypad (17 buttons) and an EditText view. If text is given, prompt the user with a message that complains about the error. Toast.makeToast(getApplicationContext() , "er ...

  • 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