diff --git a/cs654/lab5/partII/ChatClient.java b/cs654/lab5/partII/ChatClient.java index 2fa8cb9..a5b6289 100644 --- a/cs654/lab5/partII/ChatClient.java +++ b/cs654/lab5/partII/ChatClient.java @@ -32,12 +32,12 @@ public class ChatClient { // waiting for and receiving messages - while (true) { - byte[] buffer = new byte[1000]; -? -? - String message = new String(datagram.getData()); - gui.output.append(message); + for (;;) { + byte[] buffer = new byte[1000]; + DatagramPacket datagram = new DatagramPacket(buffer, buffer.length); + socket.receive(datagram); + String message = new String(datagram.getData()); + gui.output.append(message); } } catch (IOException e) { e.printStackTrace(); @@ -47,9 +47,9 @@ public class ChatClient { public void sendTextToChat(String message) { message = name+": "+message+"\n"; byte[] buf = (message).getBytes(); - ? + DatagramPacket dg = new DatagramPacket(buf, buf.length); try { - ? + socket.send(dg); } catch (IOException ex) { System.out.println(ex); diff --git a/cs654/lab5/partII/ExitListener.java b/cs654/lab5/partII/ExitListener.java new file mode 100644 index 0000000..008ab1c --- /dev/null +++ b/cs654/lab5/partII/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); + } +}