From 55e67017fcf6420a4823bb548cc2fddf99eb3517 Mon Sep 17 00:00:00 2001 From: josh Date: Sun, 17 Feb 2008 02:56:05 +0000 Subject: [PATCH] initial import of provided code git-svn-id: svn://anubis/gvsu@14 45c1a28c-8058-47b2-ae61-ca45b979098e --- cs654/lab5/partI/ChatClient.java | 69 +++++++++++++++++++++++++++++ cs654/lab5/partI/ChatFrame.java | 27 +++++++++++ cs654/lab5/partI/ChatHandler.java | 60 +++++++++++++++++++++++++ cs654/lab5/partI/ChatServer.java | 25 +++++++++++ cs654/lab5/partI/EnterListener.java | 19 ++++++++ cs654/lab5/partI/ExitListener.java | 15 +++++++ 6 files changed, 215 insertions(+) create mode 100644 cs654/lab5/partI/ChatClient.java create mode 100644 cs654/lab5/partI/ChatFrame.java create mode 100644 cs654/lab5/partI/ChatHandler.java create mode 100644 cs654/lab5/partI/ChatServer.java create mode 100644 cs654/lab5/partI/EnterListener.java create mode 100644 cs654/lab5/partI/ExitListener.java diff --git a/cs654/lab5/partI/ChatClient.java b/cs654/lab5/partI/ChatClient.java new file mode 100644 index 0000000..9324e83 --- /dev/null +++ b/cs654/lab5/partI/ChatClient.java @@ -0,0 +1,69 @@ +import java.net.*; +import java.io.*; +import java.awt.event.*; + +public class ChatClient { + + public ChatFrame gui; + + private Socket socket; + private DataInputStream in; + private DataOutputStream out; + + public ChatClient(String name, String server, int port) { + + // GUI Create GUI and handle events: + // After text input, sendTextToChat() is called, + // When closing the window, disconnect() is called. + + gui = new ChatFrame("Chat with Sockets"); + gui.input.addKeyListener (new EnterListener(this,gui)); + gui.addWindowListener(new ExitListener(this)); + + + + + + + + // create a socket, register and listen t the server + + try { + + socket = new Socket(server,port); +? +? + out.writeUTF(name); + while (true) { + gui.output.append("\n"+in.readUTF()); + }; + } catch (Exception e) { + e.printStackTrace(); + } + } + + + protected void sendTextToChat(String str) { + try { + ? + } catch (IOException e) { + e.printStackTrace(); + } + } + + protected void disconnect() { + try { + ? + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void main (String args[])throws IOException { + if (args.length!=3) + throw new RuntimeException ("Syntax: java ChatClient "); + int port=Integer.parseInt(args[2]); + ChatClient c=new ChatClient(args[0], args[1], port); + } +} + diff --git a/cs654/lab5/partI/ChatFrame.java b/cs654/lab5/partI/ChatFrame.java new file mode 100644 index 0000000..d20d6c5 --- /dev/null +++ b/cs654/lab5/partI/ChatFrame.java @@ -0,0 +1,27 @@ +import java.awt.*; + +public class ChatFrame extends Frame { + + protected TextArea output; + protected TextField input; + + protected Thread listener; + + public ChatFrame (String title){ + super (title); + + setLayout (new BorderLayout ()); + add ("Center", output = new TextArea ()); + output.setEditable (false); + add ("South", input = new TextField ()); + + pack (); + show (); + input.requestFocus (); + } + + public static void main (String args[]) { + new ChatFrame("Chat "); + } +} + diff --git a/cs654/lab5/partI/ChatHandler.java b/cs654/lab5/partI/ChatHandler.java new file mode 100644 index 0000000..caa64e2 --- /dev/null +++ b/cs654/lab5/partI/ChatHandler.java @@ -0,0 +1,60 @@ +import java.net.*; +import java.io.*; +import java.util.*; + +public class ChatHandler extends Thread { + + Socket socket; + DataInputStream in; + DataOutputStream out; + String name; + protected static Vector handlers = new Vector (); + + public ChatHandler (String name, Socket socket) throws IOException { + this.name = name; + this.socket = socket; + in = new DataInputStream (?); + out = new DataOutputStream (?); + } + + public void run () { + + try { + broadcast(name+" entered"); + handlers.addElement (this); + + while (true) { +? + broadcast(name+": "+message); + } + + } catch (IOException ex) { + System.out.println("-- Connection to user lost."); + } finally { + handlers.removeElement (this); + ? + try { + ? + } catch (IOException ex) { + System.out.println("-- Socket to user already closed ?"); + } + } + } + + + protected static void broadcast (String message) { + synchronized (handlers) { + Enumeration e = handlers.elements (); + while (e.?) { + ChatHandler handler = (ChatHandler) e.nextElement (); + try { + ? + ? + } catch (IOException ex) { + handler.stop (); + } + } + } + } +} + diff --git a/cs654/lab5/partI/ChatServer.java b/cs654/lab5/partI/ChatServer.java new file mode 100644 index 0000000..4c0a605 --- /dev/null +++ b/cs654/lab5/partI/ChatServer.java @@ -0,0 +1,25 @@ +import java.net.*; +import java.io.*; +import java.util.*; + +public class ChatServer { + + public ChatServer (int port) throws IOException { + ServerSocket server = new ServerSocket (port); + while (true) { + Socket client = server.accept(); + ? + String name = in.readUTF(); + + System.out.println ("New client "+name+" from " + ?); + ChatHandler c = new ChatHandler (name, client); + c.start (); + } + } + + public static void main (String args[]) throws IOException { + if (args.length != 1) + throw new RuntimeException ("Syntax: java ChatServer "); + new ChatServer (Integer.parseInt (args[0])); + } +} diff --git a/cs654/lab5/partI/EnterListener.java b/cs654/lab5/partI/EnterListener.java new file mode 100644 index 0000000..f829fad --- /dev/null +++ b/cs654/lab5/partI/EnterListener.java @@ -0,0 +1,19 @@ +import java.awt.event.*; + +public class EnterListener extends KeyAdapter { + + ChatClient client; + ChatFrame gui; + + public EnterListener (ChatClient client, ChatFrame gui) { + this.client = client; + this.gui = gui; + } + + public void keyPressed(KeyEvent e) { + if (e.getKeyCode()==KeyEvent.VK_ENTER) { + client.sendTextToChat(gui.input.getText()); + gui.input.setText(""); + } + } +} diff --git a/cs654/lab5/partI/ExitListener.java b/cs654/lab5/partI/ExitListener.java new file mode 100644 index 0000000..008ab1c --- /dev/null +++ b/cs654/lab5/partI/ExitListener.java @@ -0,0 +1,15 @@ +import java.awt.event.*; + +public class ExitListener extends WindowAdapter { + + ChatClient client; + + public ExitListener(ChatClient client) { + this.client = client; + } + + public void windowClosing(WindowEvent e) { + client.disconnect(); + System.exit(0); + } +}