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,
|
// After text input, sendTextToChat() is called,
|
||||||
// When closing the window, disconnect() is called.
|
// When closing the window, disconnect() is called.
|
||||||
|
|
||||||
gui = new ChatFrame("Chat with Sockets");
|
gui = new ChatFrame("Chat with Sockets");
|
||||||
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,17 +48,17 @@ public class ChatClient {
|
|||||||
|
|
||||||
protected void disconnect() {
|
protected void disconnect() {
|
||||||
try {
|
try {
|
||||||
?
|
socket.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main (String args[])throws IOException {
|
public static void main (String args[]) throws IOException {
|
||||||
if (args.length!=3)
|
if (args.length != 3)
|
||||||
throw new RuntimeException ("Syntax: java ChatClient <name> <serverhost> <port>");
|
throw new RuntimeException ("Syntax: java ChatClient <name> <serverhost> <port>");
|
||||||
int port=Integer.parseInt(args[2]);
|
int port = Integer.parseInt(args[2]);
|
||||||
ChatClient c=new ChatClient(args[0], args[1], port);
|
ChatClient c = new ChatClient(args[0], args[1], port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,33 +8,33 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
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 ();
|
||||||
}
|
}
|
||||||
|
@ -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 ();
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ public class EnterListener extends KeyAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void keyPressed(KeyEvent e) {
|
public void keyPressed(KeyEvent e) {
|
||||||
if (e.getKeyCode()==KeyEvent.VK_ENTER) {
|
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
|
||||||
client.sendTextToChat(gui.input.getText());
|
client.sendTextToChat(gui.input.getText());
|
||||||
gui.input.setText("");
|
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