cs654/lab5: chat working
git-svn-id: svn://anubis/gvsu@15 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
parent
55e67017fc
commit
2feecca1e6
@ -16,27 +16,22 @@ public class ChatClient {
|
||||
// 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 = 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);
|
||||
?
|
||||
?
|
||||
socket = new Socket(server, port);
|
||||
in = new DataInputStream(socket.getInputStream());
|
||||
out = new DataOutputStream(socket.getOutputStream());
|
||||
|
||||
out.writeUTF(name);
|
||||
while (true) {
|
||||
gui.output.append("\n"+in.readUTF());
|
||||
};
|
||||
for (;;) {
|
||||
gui.output.append("\n" + in.readUTF());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -45,7 +40,7 @@ public class ChatClient {
|
||||
|
||||
protected void sendTextToChat(String str) {
|
||||
try {
|
||||
?
|
||||
out.writeUTF(str);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -53,17 +48,17 @@ public class ChatClient {
|
||||
|
||||
protected void disconnect() {
|
||||
try {
|
||||
?
|
||||
socket.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main (String args[])throws IOException {
|
||||
if (args.length!=3)
|
||||
public static void main (String args[]) throws IOException {
|
||||
if (args.length != 3)
|
||||
throw new RuntimeException ("Syntax: java ChatClient <name> <serverhost> <port>");
|
||||
int port=Integer.parseInt(args[2]);
|
||||
ChatClient c=new ChatClient(args[0], args[1], port);
|
||||
int port = Integer.parseInt(args[2]);
|
||||
ChatClient c = new ChatClient(args[0], args[1], port);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,33 +8,33 @@ public class ChatHandler extends Thread {
|
||||
DataInputStream in;
|
||||
DataOutputStream out;
|
||||
String name;
|
||||
protected static Vector handlers = new Vector ();
|
||||
protected static Vector<ChatHandler> handlers = new Vector<ChatHandler>();
|
||||
|
||||
public ChatHandler (String name, Socket socket) throws IOException {
|
||||
this.name = name;
|
||||
this.socket = socket;
|
||||
in = new DataInputStream (?);
|
||||
out = new DataOutputStream (?);
|
||||
in = new DataInputStream (socket.getInputStream());
|
||||
out = new DataOutputStream (socket.getOutputStream());
|
||||
}
|
||||
|
||||
public void run () {
|
||||
|
||||
try {
|
||||
broadcast(name+" entered");
|
||||
broadcast("** " + name + " entered chat **");
|
||||
handlers.addElement (this);
|
||||
|
||||
while (true) {
|
||||
?
|
||||
broadcast(name+": "+message);
|
||||
String message = in.readUTF();
|
||||
broadcast(name+": " + message);
|
||||
}
|
||||
|
||||
} catch (IOException ex) {
|
||||
System.out.println("-- Connection to user lost.");
|
||||
} finally {
|
||||
handlers.removeElement (this);
|
||||
?
|
||||
broadcast("** " + name + " left chat **");
|
||||
try {
|
||||
?
|
||||
socket.close();
|
||||
} catch (IOException ex) {
|
||||
System.out.println("-- Socket to user already closed ?");
|
||||
}
|
||||
@ -44,12 +44,9 @@ public class ChatHandler extends Thread {
|
||||
|
||||
protected static void broadcast (String message) {
|
||||
synchronized (handlers) {
|
||||
Enumeration e = handlers.elements ();
|
||||
while (e.?) {
|
||||
ChatHandler handler = (ChatHandler) e.nextElement ();
|
||||
for (ChatHandler handler : handlers) {
|
||||
try {
|
||||
?
|
||||
?
|
||||
handler.out.writeUTF(message);
|
||||
} catch (IOException ex) {
|
||||
handler.stop ();
|
||||
}
|
||||
|
@ -8,10 +8,10 @@ public class ChatServer {
|
||||
ServerSocket server = new ServerSocket (port);
|
||||
while (true) {
|
||||
Socket client = server.accept();
|
||||
?
|
||||
DataInputStream in = new DataInputStream(client.getInputStream());
|
||||
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);
|
||||
c.start ();
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ public class EnterListener extends KeyAdapter {
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent e) {
|
||||
if (e.getKeyCode()==KeyEvent.VK_ENTER) {
|
||||
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
|
||||
client.sendTextToChat(gui.input.getText());
|
||||
gui.input.setText("");
|
||||
}
|
||||
|
6
cs654/lab5/partI/Makefile
Normal file
6
cs654/lab5/partI/Makefile
Normal file
@ -0,0 +1,6 @@
|
||||
|
||||
all:
|
||||
javac -deprecation *.java
|
||||
|
||||
clean:
|
||||
-rm -f *.class *~
|
Loading…
x
Reference in New Issue
Block a user