:::::::::::::: ChatApplet.java :::::::::::::: import java.applet.*; import java.awt.*; import java.awt.event.*; public class ChatApplet extends Applet implements ActionListener { public void actionPerformed(ActionEvent event) { try { String cmd = event.getActionCommand(); Update alpha = new Update(owner.getClientName(), cmd, null); owner.broadcast(alpha); } catch (Exception e) { } } ChatFrame owner; public void setOwner(ChatFrame chatFrame) { this.owner = chatFrame; } TextArea textArea; TextField textField; DrawPad drawPad; Button list, clearDraw, clearText; public void init() { this.setLayout(new BorderLayout()); Panel panel = new Panel(); panel.setLayout(new FlowLayout()); panel.add(list = new Button("List")); panel.add(clearDraw = new Button("ClearDraw")); panel.add(clearText = new Button("ClearText")); Button disconnect = new Button("disconnect"); disconnect.setBackground(Color.pink); panel.add(disconnect); textArea = new TextArea(4, 40); textArea.setEditable(false); textField = new TextField(40); drawPad = new DrawPad(this); drawPad.setSize(400, 400); add(drawPad, "East"); add(textArea, "Center"); add(textField, "South"); add(panel, "North"); textField.addActionListener(this); list.addActionListener(this); clearDraw.addActionListener(this); clearText.addActionListener(this); disconnect.addActionListener(this); } public void showText(String sender, String message) { this.textArea.append(sender + message + "\n"); this.textField.setText(""); } public void clearText() { this.textArea.setText(""); this.textField.setText(""); } } :::::::::::::: ChatFrame.java :::::::::::::: import java.awt.*; import java.rmi.*; public class ChatFrame extends Frame { ClientImplementation client; ChatApplet chatApplet; public ChatFrame(ClientImplementation client) { super(); this.client = client; this.chatApplet = new ChatApplet(); this.chatApplet.setOwner(this); this.chatApplet.init(); this.add(this.chatApplet, "Center"); } public void broadcast(Update event) { try { this.client.getServer().broadcast(event); } catch (Exception e) { } } public String getClientName() { return this.client.name; } } :::::::::::::: Client.java :::::::::::::: import java.rmi.*; public interface Client extends Remote { public void update(Update event) throws RemoteException; public String name() throws RemoteException; public void setId(int id) throws RemoteException; public void setServer(Server server) throws RemoteException; } :::::::::::::: ClientImplementation.java :::::::::::::: import java.rmi.*; import java.awt.event.*; public class ClientImplementation implements Client { String name; int id; Server server; public Server getServer() { return server; } ChatFrame chatFrame; public ClientImplementation(String name) { this.name = name; this.chatFrame = new ChatFrame(this); chatFrame.setSize(820, 600); chatFrame.show(); chatFrame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); } public void update(Update event) throws RemoteException { if (event.line != null) chatFrame.chatApplet.drawPad.receive(event.line); if (event.message != null) { if (event.message.equals("ClearDraw")) { chatFrame.chatApplet.drawPad.clearScreen(); this.chatFrame.chatApplet.textField.setText(""); } else if (event.message.equals("ClearText")) { chatFrame.chatApplet.clearText(); } else if (event.message.equals("List")) { chatFrame.chatApplet.showText (event.sender + " is listing: \n", server.list()); } else { // basic message chatFrame.chatApplet.showText (event.sender + ": ", event.message); } } } public String name() throws RemoteException { return this.name; } public void setId(int id) throws RemoteException { this.id = id; System.out.println(this.name + ": my id is " + this.id); } public void setServer(Server server) throws RemoteException { this.server = server; } } :::::::::::::: DrawPad.java :::::::::::::: import java.awt.*; import java.awt.event.*; import java.util.*; public class DrawPad extends Canvas implements MouseMotionListener, MouseListener { Vector lines = new Vector(); ChatApplet applet; public DrawPad(ChatApplet applet) { this.applet = applet; addMouseMotionListener(this); addMouseListener(this); } int startX, startY; public void mouseDragged(MouseEvent event) { Line line = new Line(startX, startY, event.getX(), event.getY()); startX = event.getX(); startY = event.getY(); Update update = new Update(applet.owner.client.name, null, line); try { applet.owner.broadcast(update); } catch (Exception exception) { } } public void receive(Line line) { lines.addElement(line); this.render(line); } public void clearScreen() { this.lines = new Vector(); Graphics g = this.getGraphics(); this.repaint(); } public void render(Line line) { this.getGraphics().drawLine(line.x0, line.y0, line.x1, line.y1); } public void mouseMoved(MouseEvent e) { } public void mousePressed(MouseEvent e) { startX = e.getX(); startY = e.getY(); } public void mouseReleased(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void paint(Graphics g) { if (lines != null) { for (int i = 0; i < lines.size(); i++) { this.render((Line)lines.elementAt(i)); } } } } :::::::::::::: Line.java :::::::::::::: import java.io.*; public class Line implements Serializable { int x0, y0, x1, y1; public Line(int x0, int y0, int x1, int y1) { this.x0 = x0; this.y0 = y0; this.x1 = x1; this.y1 = y1; } } :::::::::::::: NetworkClient.java :::::::::::::: import java.rmi.*; import java.rmi.server.*; public class NetworkClient extends ClientImplementation implements Client { public NetworkClient(String name) throws RemoteException { super(name); UnicastRemoteObject.exportObject(this); } public static void main(String[] args) { try { Server far = (Server)Naming.lookup( "rmi://" + args[0] + ":" + args[1] + "/Dirac"); NetworkClient here = new NetworkClient(args[2]); here.setId(far.register(here)); here.setServer(far); // here.start(); } catch (Exception e) { System.out.println("Error in client... " + e); e.printStackTrace(); } } } :::::::::::::: NetworkServer.java :::::::::::::: import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.*; public class NetworkServer extends ServerImplementation implements Server { public NetworkServer() throws RemoteException { UnicastRemoteObject.exportObject(this); System.out.println("Server being initialized... "); } public static void main(String[] args) { System.setSecurityManager(new RMISecurityManager()); try { NetworkServer pam = new NetworkServer(); Registry cat = LocateRegistry.createRegistry(Integer.parseInt(args[0])); cat.bind("Dirac", pam); System.out.println("Server is ready... "); } catch (Exception e) { System.out.println("Server error: " + e + "... "); } } } :::::::::::::: Server.java :::::::::::::: import java.rmi.*; public interface Server extends Remote { public int register(Client client) throws RemoteException; public void broadcast(Update event) throws RemoteException; public String list() throws RemoteException; } :::::::::::::: ServerImplementation.java :::::::::::::: import java.rmi.*; public class ServerImplementation implements Server { Client clients[] = new Client[100]; int index = -1; public int register(Client client) throws RemoteException { clients[++index] = client; return index; } public void broadcast(Update event) throws RemoteException { for (int i = 0; i <= index; i++) clients[i].update(event); } public String list() throws RemoteException { String result = ""; for (int i = 0; i <= index; i++) result += " ***(" + clients[i].name() + ")*** \n"; return result; } } :::::::::::::: Simulation.java :::::::::::::: import java.rmi.*; public class Simulation { public static void main(String[] args) throws RemoteException { ServerImplementation server = new ServerImplementation(); Server far = server; ClientImplementation one = new ClientImplementation("One"); one.setId(far.register(one)); one.setServer(far); ClientImplementation two = new ClientImplementation("Two"); two.setId(far.register(two)); two.setServer(far); } } :::::::::::::: Update.java :::::::::::::: import java.io.*; public class Update implements Serializable { String sender; String message; Line line; public Update(String sender, String message, Line line) { this.sender = sender; this.message = message; this.line = line; } public String toString() { return sender + ": " + message + " " + line; } }