gvsu/cs654/lab5/partI/ChatHandler.java
josh 55e67017fc initial import of provided code
git-svn-id: svn://anubis/gvsu@14 45c1a28c-8058-47b2-ae61-ca45b979098e
2008-02-17 02:56:05 +00:00

61 lines
1.4 KiB
Java

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 ();
}
}
}
}
}