diff --git a/cs654/lab5/partII/ChatClient.java b/cs654/lab5/partII/ChatClient.java new file mode 100644 index 0000000..2fa8cb9 --- /dev/null +++ b/cs654/lab5/partII/ChatClient.java @@ -0,0 +1,67 @@ +import java.net.*; +import java.io.*; + +public class ChatClient { + + ChatFrame gui; + String name; + + InetAddress group; + MulticastSocket socket; + int port = 6789; + + public ChatClient(String name) { + this.name = name; + + // GUI Create GUI and handle events: + // After text input, sendTextToChat() is called, + // When closing the window, disconnect() is called. + + gui = new ChatFrame("Chat with IP-Multicast"); + gui.input.addKeyListener (new EnterListener(this,gui)); + gui.addWindowListener(new ExitListener(this)); + +// In order for a host to receive a multicast data, +// the receiver must register with a group, by creating a MulticastSocket on a port and call the joinGroup() method. + + try { + socket = new MulticastSocket(port); + group = InetAddress.getByName("226.1.3.5"); + socket.joinGroup(group); + gui.output.append("Connected...\n"); + + // waiting for and receiving messages + + while (true) { + byte[] buffer = new byte[1000]; +? +? + String message = new String(datagram.getData()); + gui.output.append(message); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + public void sendTextToChat(String message) { + message = name+": "+message+"\n"; + byte[] buf = (message).getBytes(); + ? + try { + ? + } + catch (IOException ex) { + System.out.println(ex); + } + } + + public void disconnect() {} + + public static void main(String args[]) { + if (args.length!=1) + throw new RuntimeException ("Syntax: java ChatClient "); + ChatClient client = new ChatClient(args[0]); + } +} +