// ChatGui - a chat client program utilizing a simple GUI. The // program initializes the GUI with a JTextField for chat input // and a JTextArea for displaying messages sent and received. // Two sockets are utilized: a TCP socket for receiving // connection info and messages from the server, and a UDP // socket for sending messages to the server which distributes them // to other clients. The main loop is the receiveMessages() method, // which loops until termination waiting on messages from the // server. This loop is interrupted by a keyboard event to // send this user's message via the sendMessage() method. // Run ChatGui with: // java ChatGui import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.net.*; public class ChatGui extends JFrame { private JTextField enter; private JTextArea display; private Socket connectionTCPSocket; private BufferedReader fromServer; private DatagramSocket chatOutSocket; private int chatOutUDPPort; private InetAddress chatServerIP; private String screenName; public ChatGui(String args[]) { super(args[0] + ": Chat Client" ); Container c = getContentPane(); // set up JtextField for input with ActionListener enter = new JTextField(); enter.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { sendMessage( e.getActionCommand() ); } } ); c.add( enter, BorderLayout.NORTH ); // JTextArea for sent and received messages display = new JTextArea(); c.add( new JScrollPane( display ), BorderLayout.CENTER ); setSize( 400, 300 ); show(); enter.setEnabled( true ); initSession(args); // initializes sockets and "logs in" to server } // sendMessage - sends the string "message" to the server via UDP socket, // displays the message in the JTextArea, and finally clears the JTextField // input areea for the next message. private void sendMessage( String message ) { // create a datagram consisting of the user's screen name, two separator // characters and the message try { // write the chat message to the server via UDP socket then display locally display.append( "\n" + chatToSend ); // display in JTextArea enter.setText(""); //Clear the JTextField } catch(IOException ioException) { System.err.println ("IOException in sendMessage."); } } // receiveMessages - loops forever waiting on messages from server via the // TCP connection. When a message is received, display it in JTextArea private void receiveMessages() { //loop waiting for TCP messages from server, then display them while (true) { try { ///Get and display chat messages } catch(IOException ioException) { System.err.println ("IOException in receiveMessage loop."); break; } } } // initSession - initializes the two sockets with the server. TCP port is // used to receive messages from the server, UDP port is used to send chat messages // to the server. Recieves command line arguments: screen name, and TCP port. Uses // chatServerHostName to get server IP address. If selected screen name is already // in use on server, UDP port # received from server is 0, which invokes error // MessageDialog to inform user, then shuts down ChatGui. private void initSession(String args[]) { screenName = args[0]; String chatServerHostName = args[1]; int chatServerTCPPort = Integer.parseInt(args[2]); DataOutputStream toServer; try { // Set up TCP in/out buffers and instantiate TCP socket // Send screen name and other info to server // Get UDP port number from server } catch (IOException ioException) { ioException.printStackTrace(); } } // Execute with args: screenName, chatServerHostName, chatServerTCPPort public static void main( String args[] ) { if (args.length != 3) { System.err.println("Usage: ChatGUI . Try again."); System.exit (-1); } ChatGui application = new ChatGui(args); // The following listener responds to the close event on the window application.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); application.receiveMessages(); } }