Source of Client.java


  1: // Fig. 24.11: Client.java
  2: // Client that sends and receives packets to/from a server.
  3: import java.io.IOException;
  4: import java.net.DatagramPacket;
  5: import java.net.DatagramSocket;
  6: import java.net.InetAddress;
  7: import java.net.SocketException;
  8: import java.awt.BorderLayout;
  9: import java.awt.event.ActionEvent;
 10: import java.awt.event.ActionListener;
 11: import javax.swing.JFrame;
 12: import javax.swing.JScrollPane;
 13: import javax.swing.JTextArea;
 14: import javax.swing.JTextField;
 15: import javax.swing.SwingUtilities;
 16: 
 17: public class Client extends JFrame 
 18: {
 19:    private JTextField enterField; // for entering messages
 20:    private JTextArea displayArea; // for displaying messages
 21:    private DatagramSocket socket; // socket to connect to server
 22: 
 23:    // set up GUI and DatagramSocket
 24:    public Client()
 25:    {
 26:       super( "Client" );
 27: 
 28:       enterField = new JTextField( "Type message here" );
 29:       enterField.addActionListener(
 30:          new ActionListener() 
 31:          { 
 32:             public void actionPerformed( ActionEvent event )
 33:             {
 34:                try // create and send packet
 35:                {
 36:                   // get message from textfield 
 37:                   String message = event.getActionCommand();
 38:                   displayArea.append( "\nSending packet containing: " +
 39:                      message + "\n" );
 40: 
 41:                   byte data[] = message.getBytes(); // convert to bytes
 42:          
 43:                   // create sendPacket
 44:                   DatagramPacket sendPacket = new DatagramPacket( data, 
 45:                      data.length, InetAddress.getLocalHost(), 5000 );
 46: 
 47:                   socket.send( sendPacket ); // send packet
 48:                   displayArea.append( "Packet sent\n" );
 49:                   displayArea.setCaretPosition( 
 50:                      displayArea.getText().length() );
 51:                } // end try
 52:                catch ( IOException ioException ) 
 53:                {
 54:                   displayMessage( ioException.toString() + "\n" );
 55:                   ioException.printStackTrace();
 56:                } // end catch
 57:             } // end actionPerformed
 58:          } // end inner class
 59:       ); // end call to addActionListener
 60: 
 61:       add( enterField, BorderLayout.NORTH );
 62: 
 63:       displayArea = new JTextArea();
 64:       add( new JScrollPane( displayArea ), BorderLayout.CENTER );
 65: 
 66:       setSize( 400, 300 ); // set window size
 67:       setVisible( true ); // show window
 68: 
 69:       try // create DatagramSocket for sending and receiving packets
 70:       {
 71:          socket = new DatagramSocket();
 72:       } // end try
 73:       catch ( SocketException socketException ) 
 74:       {
 75:          socketException.printStackTrace();
 76:          System.exit( 1 );
 77:       } // end catch
 78:    } // end Client constructor
 79: 
 80:    // wait for packets to arrive from Server, display packet contents
 81:    public void waitForPackets()
 82:    {
 83:       while ( true ) 
 84:       {
 85:          try // receive packet and display contents
 86:          {
 87:             byte data[] = new byte[ 100 ]; // set up packet
 88:             DatagramPacket receivePacket = new DatagramPacket( 
 89:                data, data.length );
 90: 
 91:             socket.receive( receivePacket ); // wait for packet
 92: 
 93:             // display packet contents
 94:             displayMessage( "\nPacket received:" + 
 95:                "\nFrom host: " + receivePacket.getAddress() + 
 96:                "\nHost port: " + receivePacket.getPort() + 
 97:                "\nLength: " + receivePacket.getLength() + 
 98:                "\nContaining:\n\t" + new String( receivePacket.getData(), 
 99:                   0, receivePacket.getLength() ) );
100:          } // end try
101:          catch ( IOException exception ) 
102:          {
103:             displayMessage( exception.toString() + "\n" );
104:             exception.printStackTrace();
105:          } // end catch
106:       } // end while
107:    } // end method waitForPackets
108: 
109:    // manipulates displayArea in the event-dispatch thread
110:    private void displayMessage( final String messageToDisplay )
111:    {
112:       SwingUtilities.invokeLater(
113:          new Runnable()
114:          {
115:             public void run() // updates displayArea
116:             {
117:                displayArea.append( messageToDisplay );
118:             } // end method run
119:          }  // end inner class
120:       ); // end call to SwingUtilities.invokeLater
121:    } // end method displayMessage
122: }  // end class Client
123: 
124: 
125: /**************************************************************************
126:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
127:  * Pearson Education, Inc. All Rights Reserved.                           *
128:  *                                                                        *
129:  * DISCLAIMER: The authors and publisher of this book have used their     *
130:  * best efforts in preparing the book. These efforts include the          *
131:  * development, research, and testing of the theories and programs        *
132:  * to determine their effectiveness. The authors and publisher make       *
133:  * no warranty of any kind, expressed or implied, with regard to these    *
134:  * programs or to the documentation contained in these books. The authors *
135:  * and publisher shall not be liable in any event for incidental or       *
136:  * consequential damages in connection with, or arising out of, the       *
137:  * furnishing, performance, or use of these programs.                     *
138:  *************************************************************************/