You are to implement a complete chat system as discussed in class. The starting files, ChatServer.java, DataObject.java and ChatFrame.java are in the project directory of the course web site along with a screenshot of the demonstration given in class. You must modify the starting files to add the following features:

  • Sending and receiving messages
  • Connect and disconnect client to server
  • Announce entrances and exits
  • Prepend user name to messages
  • Proper user list
  • Private messages

ChatFrame.java

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.*;

public class ChatFrame extends Frame{
public ChatFrame(){
setSize(500, 500);
setTitle("Chat Frame");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
add(new ChatPanel(), BorderLayout.CENTER);
setVisible(true);
}
public static void main(String[] args){
new ChatFrame();
}
}

class ChatPanel extends Panel implements ActionListener, Runnable{ //INCOMPLETE!!!
TextArea ta;
TextField tf;
Button connect, disconnect;
Thread thread;
java.awt.List list;

public ChatPanel(){
setLayout(new BorderLayout());
tf = new TextField();
tf.addActionListener(this);
tf.addActionListener(this);
ta = new TextArea();
add(tf, BorderLayout.NORTH);
add(ta, BorderLayout.CENTER);
connect = new Button("Connect");
disconnect = new Button("Disconnect");
Panel buttonPanel = new Panel();
buttonPanel.add(connect);
buttonPanel.add(disconnect);
add(buttonPanel, BorderLayout.SOUTH);
list = new java.awt.List(4, true);
add(list, BorderLayout.EAST);
thread = new Thread(this);
thread.start();



}
public void actionPerformed(ActionEvent ae){
//dummy content - this is where you react to the user
System.out.println(tf.getText());
tf.setText("");
}
public void run(){
//dummy content - replace this
System.out.println("This is the thread. I should be listeneing for messages from the server.");
}
}

ChatServer.java

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

public class ChatServer{
public static void main(String[] args ){
ArrayList< ChatHandler>handlers = new ArrayList< ChatHandler>();
try{
ServerSocket s = new ServerSocket(3000);
for(;;){
Socket incoming = s.accept();
new ChatHandler(incoming, handlers).start();
}
}catch (Exception e){
System.out.println(e);
}
}
}

class ChatHandler extends Thread{
DataObject myObject = null;
private Socket incoming;
ArrayList< ChatHandler>handlers;
ObjectInputStream in;
ObjectOutputStream out;

public ChatHandler(Socket i, ArrayListh){
incoming = i;
handlers = h;
handlers.add(this);
}
public void run(){
try{
in = new ObjectInputStream(incoming.getInputStream());
out = new ObjectOutputStream(incoming.getOutputStream());

boolean done = false;
while (!done){
DataObject objIn = (DataObject)in.readObject();
if (objIn == null){
done = true;
}else{
for(ChatHandler h : handlers){
h.out.writeObject(objIn);
}
if (objIn.getMessage().trim().equals("BYE")){
done = true;
}
}
}
incoming.close();
}catch (Exception e){
System.out.println(e);
}finally{
handlers.remove(this);
}
}
}

DataObject.java

import java.io.*;
import java.util.*;

public class DataObject implements Serializable{

private String message;
private ArrayList< String> names;
private String to;

DataObject(){
message = "";
names = new ArrayList< String>();
}

public String getMessage(){
return message;
}

public void setMessage(String inMessage){
message = inMessage;
}
public ArrayList< String> getNames(){
return names;
}
public void setNames(ArrayList< String> names){
this.names = names;
}
public void setTo(String name){
to = name;
}
public String getTo(){
return to;
}
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.