Source of Server.java


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