Question
Develop a simple multithreaded chat service using JAVA. Programs will run from the command line. The server can accept multiple connections at once. The server
Develop a simple multithreaded chat service using JAVA. Programs will run from the command line. The server can accept multiple connections at once. The server program will log and broadcast every chat message it receives. For each connection, the server will create a new thread and hand the connection to it then waits. The server program will create a chat file called xy_chat.txt upon first connection (first client joins chat room) and share with subsequent connections. The server will delete the chat file when the last connection is closed (everyone left). The server will create a new chat file each time a client joins an empty chat room. Since a client can read from and write to the chat file, the activities of these threads must be synchronized. A thread must have exclusive access to the chat file before writing to it.
When a connection is made, the thread that manages the connection will wait for the users name, and then sends the client the content of the chat file. It will also log and broadcast the arrival of a new client to the chat room. Any message from the client after this point, except DONE, will be logged and broadcasted. Note: the server doesnt echo the message back to the same client. For DONE, the thread will log and broadcast the departure of the client from the chat room. When the client signals that it is ready to leave the chat session, the thread will close the client connection, and then will terminate.
The client program must have two threads in order to implement a full duplex communication. One thread sends whatever the user types at the keyboard to the server and the other thread displays whatever it receives from the server. The user can leave the chat room at any time by typing DONE at the keyboard, and the client program will send that message to the server and then terminate gracefully. Modify the following Client/Server files
// Multi-threaded Server program
// TCPServerMT.java
import java.io.*;
import java.net.*;
public class TCPServerMT
{
private static ServerSocket ss;
public static void main(String[] args)
{
System.out.println("Opening port... ");
try{
// Create a server object
ss = new ServerSocket(Integer.parseInt(args[0]));
}
catch(IOException e){
servSock = new ServerSocket(20001);
}
do
{
run();
}while (true);
}
private static void run()
{
Socket link = null;
try
{
// Put the server into a waiting state
link = servSock.accept();
// print local host name
String host = InetAddress.getLocalHost().getHostName();
System.out.println("Client has estabished a connection to " + host);
// Create a thread to handle this connection
ClientHandler handler = new ClientHandler(link);
// start serving this connection
handler.start();
}
catch(IOException e){
e.printStackTrace();
}
}
}
class ClientHandler extends Thread
{
private Socket client;
private BufferedReader in;
private PrintWriter out;
public ClientHandler(Socket s)
{
// set up the socket
client = s;
try
{
// Set up input and output streams for socket
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(),true);
}
catch(IOException e){
e.printStackTrace();
}
}
// overwrite the method 'run' of the Runnable interface
// this method is called automatically when a client thread starts.
public void run()
{
// Receive and process the incoming data
int numMessages = 0;
try
{
String message = in.readLine();
while (!message.equals("DONE"))
{
System.out.println(message);
numMessages ++;
message = in.readLine();
}
// Send a report back and close the connection
out.println("Server received " + numMessages + " messages");
}
catch(IOException e){
e.printStackTrace();
}
finally{
try{
System.out.println("!!!!! Closing connection... !!!!!" );
client.close();
}
catch(IOException e){
System.out.println("Unable to disconnect!");
System.exit(1);
}
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------
// Client program
// File name: TCPClient.java
import java.io.*;
import java.net.*;
import java.util.*;
public class TCPClient
{
private static InetAddress host;
private static String user;
public static void main(String[] args){
try {
// Get server IP-address
if(args[0]! = null) {
user = args[0];
}else {
Scanner key = new Scanner(System.in);
System.out.print("\Enter Name: ");
user = key.next();
}
host = InetAddress.getByName(args[0]);
}
catch(UnknownHostException e){
host = InetAddress.getLocalHost();
}
try {
int n = Integer.parseInt(args[2]);
run(n);
}
catch(Exception e) {run(20001);
}
private static void run(int port){
Socket link = null;
try{
// Establish a connection to the server
link = new Socket(host,port);
// Set up input and output streams for the connection
BufferedReader in = new BufferedReader(
new InputStreamReader(link.getInputStream()));
PrintWriter out = new PrintWriter(
link.getOutputStream(),true);
//Set up stream for keyboard entry
BufferedReader userEntry = new BufferedReader(new InputStreamReader(System.in));
String message, response;
// Get data from the user and send it to the server
do{
System.out.print("Enter message: ");
message = userEntry.readLine();
out.println(message);
}while (!message.equals("DONE"));
// Receive the final report and close the connection
response = in.readLine();
System.out.println(response);
}
catch(IOException e){
e.printStackTrace();
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started