Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this project we are going to develop a multithread chatroom in Java. Developi

ID: 3730252 • Letter: I

Question

In this project we are going to develop a multithread chatroom in Java.

Developing tools:

NetBeans in Windows, or Java JDK and in Ubuntu (You will need the permission from help desk.)

General description:

This project contains two parts, client part and server part. The chatroom will broadcast everyone’s message to everyone. Group work needs to implement the id system and @someone for a private message.

The client part should utilize java socket for network communication, including reading messages from server and writing messages to the server. The client part can have two threads: one thread reads from socket and display messages, and one thread reads from keyboard and write to the socket.

The server part should give each client a thread (for communication). Each thread will handle a client.

Developing procedure:

Have the developing environment (tools) ready.

Get familiar with socket programming and multithread programming in java.

Base on the single thread echo server, implement a multithread echo server.

Base on multithread echo server, add shared data for message broadcasting purpose.

Make two threads for client, one for reading from socket and one for writing to the socket.

Add id system and private message (required for group work, optional on individual work)

Grading:

This project will be counted as big homework and weight three times as a regular homework/quiz. You need to write your own program (copying directly from web will result zero point).

Reference:

Java tutorial (socket): https://docs.oracle.com/javase/tutorial/networking/sockets/index.html

Java tutorial (thread): https://docs.oracle.com/javase/tutorial/essential/concurrency/

Server side example: https://www.geeksforgeeks.org/multi-threaded-chat-application-set-1/

Client side example: https://www.geeksforgeeks.org/multi-threaded-chat-application-set-2/

A YouTube video: https://www.youtube.com/watch?v=Uo5DY546rKY

On github: https://github.com/search?utf8=%E2%9C%93&q=java+chatroom&type=

Explanation / Answer

package com.test.chatServer;

import java.io.*;
import java.net.*;

public class ChatroomServer
{
// port number for the server
private static final int PORT_NUM = 8189;

// the server socket:
private static ServerSocket ss;

// set when shutDown() is called to stop the server:
private static boolean shutDownCalled = false;
private static Chatter c;

// shut the server down by closing the server socket
public static void shutDown()
{
// flag that the server socket has been closed
shutDownCalled = true;

try
{
ss.close();
}
catch (Exception e)
{
// something went wrong; give data:
System.err.println("problem shutting down:");
System.err.println(e.getMessage());
// and trust the JVM to clean up:
System.exit(1);
}
}


public static void main(String[] args )
{
// for client connections
Socket incoming;
ChatterList cl = new ChatterList();
  
// session-handling thread
Thread t;
//Chatter c = null;

try
{
// set up server socket
ss = new ServerSocket(PORT_NUM);

while (true)
{

  
// format message saying we are waiting  
System.out.println("Server waiting for Clients on port " + PORT_NUM + ".");  

incoming = ss.accept(); // accept connection  
// if I was asked to stop  

  
// start session-handler in new thread
  
//ss = new Chatter();
//t = new Thread(c, "dan");  
  
t = new Thread(new Chatter(incoming, cl, c));
t.start();
  
}}

catch (SocketException se)
{
/*
* will be thrown when accept() is called after closing
* the server socket, in method shutDown().
* If shutDownCalled, then simply exit; otherwise,
* something else has happened:
*/
if (! shutDownCalled)
{
System.err.println("socket problem:");
System.err.println(se.getMessage());
// trust the JVM to clean up
System.exit(1);
}
}
catch (IOException ioe)
{
System.err.println("I/O error:");
System.err.println(ioe.getMessage());
System.exit(1);
}
finally
{
if (ss != null)
{
try
{
ss.close();
}
catch (Exception e)
{
System.err.println("closing: " + e.getMessage());
}
}
}
}
}

/*
* Session-handler class to handle one remote client
* in a separate thread.
*/

------------------------------------------------------------------------------------------------------------------------------------------

package com.test.chatServer;

import java.io.*;
import java.net.*;

class Chatter implements Runnable
{
// the connection to the remote client
BufferedReader in = null;
PrintWriter out = null;
private Socket client;
private ChatterList chatterlist;
private Chatter chatter;
String line;
ChatterList cl = new ChatterList();
String username;
Tail next;
Chatter(Socket s, ChatterList cl, Chatter c)
{
client = s;
chatterlist = cl;
chatter = c;
}
public String getname()
{
return username;
}
public void sendToUser(String line)
{  
out.print(line);
out.flush();   
}
public void run()
{
// for I/O
  
//chatterlist.addConnection();
  
  
try
{
// set up I/O
in = new BufferedReader
(new InputStreamReader(client.getInputStream()));
out = new PrintWriter
(new OutputStreamWriter(client.getOutputStream()));

// enter USERNAME
out.println("Hello! Enter name");
out.flush();

username = in.readLine(); //type user name
  
out.println("Welcome "+username);//print user name
out.flush();
cl.connect(this);

// for client input

boolean done = false;
while (!done)
{
line = in.readLine();
  

  
  
//sendToUser(line);
  
cl.broadcast(line);

if ((line == null)
|| (line.trim().equals("BYE")))
{
// quit
done = true;
}
  
  

}}
catch (IOException e)
{
// fatal error for this session
System.err.println(e.getMessage());
}
finally
{ // close connections
try
{
in.close();
}
catch(IOException e)
{
}
if (out != null)
{
out.close();
}
if (client != null)
{
try
{
client.close();
}
catch (IOException e)
{
}
}
}
}

}

class Tail{
Chatter theChatter;
Tail next;
Tail(Chatter c, Tail n)
{
theChatter = c;
next = n;
//c.next = next;
}
}

--------------------------------------------------------------------------------------------------------------------------------

package com.test.chatServer;

public class ChatterList {

private Tail thelist = null;
  

public ChatterList()
{
}

private void add(Chatter c)
{
  
  

thelist = new Tail(c, thelist);
System.out.println("new user: "+c.getname());
c.next = thelist;
  
  
}


public void broadcast(String line)
{
// variable for traversing the linked list
Tail tmp = thelist;

/*
* traverse the list
*/
while (tmp != null) // exit loop at the end of the list
{
// send the message
tmp.theChatter.sendToUser(line);
// and move on to the next chatter in the list
tmp = tmp.next;
}
}

public void connect(Chatter c)
{
// inform all other chatters
broadcast(c.getname());
// and add the new Chatter to the list
add(c);
}


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote