cs654/lab5: chat working

git-svn-id: svn://anubis/gvsu@15 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2008-02-17 03:37:15 +00:00
parent 55e67017fc
commit 2feecca1e6
5 changed files with 35 additions and 37 deletions

View File

@ -20,23 +20,18 @@ public class ChatClient {
gui.input.addKeyListener(new EnterListener(this, gui)); gui.input.addKeyListener(new EnterListener(this, gui));
gui.addWindowListener(new ExitListener(this)); gui.addWindowListener(new ExitListener(this));
// create a socket, register and listen t the server // create a socket, register and listen t the server
try { try {
socket = new Socket(server, port); socket = new Socket(server, port);
? in = new DataInputStream(socket.getInputStream());
? out = new DataOutputStream(socket.getOutputStream());
out.writeUTF(name); out.writeUTF(name);
while (true) { for (;;) {
gui.output.append("\n" + in.readUTF()); gui.output.append("\n" + in.readUTF());
}; }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -45,7 +40,7 @@ public class ChatClient {
protected void sendTextToChat(String str) { protected void sendTextToChat(String str) {
try { try {
? out.writeUTF(str);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -53,7 +48,7 @@ public class ChatClient {
protected void disconnect() { protected void disconnect() {
try { try {
? socket.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -8,23 +8,23 @@ public class ChatHandler extends Thread {
DataInputStream in; DataInputStream in;
DataOutputStream out; DataOutputStream out;
String name; String name;
protected static Vector handlers = new Vector (); protected static Vector<ChatHandler> handlers = new Vector<ChatHandler>();
public ChatHandler (String name, Socket socket) throws IOException { public ChatHandler (String name, Socket socket) throws IOException {
this.name = name; this.name = name;
this.socket = socket; this.socket = socket;
in = new DataInputStream (?); in = new DataInputStream (socket.getInputStream());
out = new DataOutputStream (?); out = new DataOutputStream (socket.getOutputStream());
} }
public void run () { public void run () {
try { try {
broadcast(name+" entered"); broadcast("** " + name + " entered chat **");
handlers.addElement (this); handlers.addElement (this);
while (true) { while (true) {
? String message = in.readUTF();
broadcast(name+": " + message); broadcast(name+": " + message);
} }
@ -32,9 +32,9 @@ public class ChatHandler extends Thread {
System.out.println("-- Connection to user lost."); System.out.println("-- Connection to user lost.");
} finally { } finally {
handlers.removeElement (this); handlers.removeElement (this);
? broadcast("** " + name + " left chat **");
try { try {
? socket.close();
} catch (IOException ex) { } catch (IOException ex) {
System.out.println("-- Socket to user already closed ?"); System.out.println("-- Socket to user already closed ?");
} }
@ -44,12 +44,9 @@ public class ChatHandler extends Thread {
protected static void broadcast (String message) { protected static void broadcast (String message) {
synchronized (handlers) { synchronized (handlers) {
Enumeration e = handlers.elements (); for (ChatHandler handler : handlers) {
while (e.?) {
ChatHandler handler = (ChatHandler) e.nextElement ();
try { try {
? handler.out.writeUTF(message);
?
} catch (IOException ex) { } catch (IOException ex) {
handler.stop (); handler.stop ();
} }

View File

@ -8,10 +8,10 @@ public class ChatServer {
ServerSocket server = new ServerSocket (port); ServerSocket server = new ServerSocket (port);
while (true) { while (true) {
Socket client = server.accept(); Socket client = server.accept();
? DataInputStream in = new DataInputStream(client.getInputStream());
String name = in.readUTF(); String name = in.readUTF();
System.out.println ("New client "+name+" from " + ?); System.out.println ("New client " + name + " from " + client.getInetAddress());
ChatHandler c = new ChatHandler (name, client); ChatHandler c = new ChatHandler (name, client);
c.start (); c.start ();
} }

View File

@ -0,0 +1,6 @@
all:
javac -deprecation *.java
clean:
-rm -f *.class *~