home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / applets / server / server.jav < prev   
Encoding:
Text File  |  1995-12-15  |  4.0 KB  |  177 lines

  1. /*
  2.  * Server.java -- Simple socket server app.
  3.  *
  4.  * Mike Fletcher, fletch@ain.bls.com
  5.  * 951027
  6.  *
  7.  */
  8.  
  9. // Import what we need
  10. import java.applet.Applet;
  11. import java.io.*;
  12. import java.net.*;
  13. import java.lang.InterruptedException;
  14.  
  15. // Listen does the work
  16. class Listen implements Runnable {
  17.   Thread t = null;        // Server thread
  18.  
  19.   public Listen( ) {
  20.     ;                // Nothing specific needed
  21.   }
  22.  
  23.   public void destroy( ) {
  24.     System.out.println( "Listen::destroy called" );
  25.   }
  26.  
  27.   // Start thread running (allocing if needed)
  28.   public synchronized void start( ) {
  29.     System.out.println( "start: t = " + t );
  30.  
  31.     if( t == null ) {
  32.       t = new Thread( this );
  33.       t.setPriority( Thread.MAX_PRIORITY / 4 );
  34.       t.start();
  35.     }
  36.   }
  37.  
  38.   // Stop thread from running if it exists
  39.   public synchronized void stop( ) {
  40.     System.out.println( "stop: t = " + t );
  41.  
  42.     if( t != null ) {
  43.       t.stop( );
  44.       t = null;
  45.     }
  46.   }
  47.  
  48.   // Allow join with our thread
  49.   public final void join( ) 
  50.     throws java.lang.InterruptedException 
  51.   {
  52.     try {
  53.       if( t != null ) {
  54.     t.join();
  55.       } 
  56.     } catch ( InterruptedException e ) {
  57.       throw e;
  58.     }
  59.  
  60.     return;
  61.   }
  62.  
  63.   // Run method to be started by Thread
  64.   public void run( ) {
  65.     ServerSocket s = null;    // Socket we're listening to.
  66.     InputStream in = null;    // Socket input stream
  67.     PrintStream out = null;    // Socket output stream
  68.     Socket con = null;        // Current connection
  69.  
  70.     // Open a new server socket on port 5000 or die
  71.     try {
  72.       s = new ServerSocket( 5000 );
  73.     } catch ( Exception e ) {
  74.       System.out.println( "Exception:\n" + e );
  75.       System.exit( 1 );        // Exit with error
  76.     }    
  77.  
  78.     // Print out our socket
  79.     System.out.println( "ServerSocket: " + s );
  80.  
  81.     // While the thread is running . . .
  82.     while( t != null ) {
  83.       // Accept an incomming connection
  84.       try {
  85.     con = s.accept( );
  86.       } catch ( Exception e ) {
  87.     System.out.println( "accept: " + e );
  88.     System.exit( 1 );    // Exit with error
  89.       }
  90.  
  91.       // Get the I/O streams from socket
  92.       try {
  93.     out = new PrintStream( con.getOutputStream() );
  94.     in = con.getInputStream();
  95.       } catch ( Exception e ) {
  96.     System.out.println( "building streams: " + e );
  97.       }
  98.  
  99.       // Print welcome on socket
  100.       out.println( "Hi there! Enter 'bye' to exit." );
  101.  
  102.       // Read what comes in on the socket and spit it back
  103.       try {
  104.     int nbytes;
  105.     boolean done = false;
  106.     byte b[] = new byte[ 1024 ];
  107.  
  108.     while(!done && ((nbytes = in.read( b, 0, 1024 )) != -1 )) {
  109.       String str = new String( b, 0, 0, nbytes );
  110.  
  111.       // Spit back what we recieved
  112.       out.println( "Recieved:\n|" + str + "|" );
  113.  
  114.       // Done if user says bye
  115.       if( str.trim().compareTo( "bye" ) == 0 ) {
  116.         done = true;
  117.       }
  118.  
  119.       // Die if they enter "DIE!"
  120.       if( str.trim().compareTo( "DIE!" ) == 0 ) {
  121.         t.stop();
  122.         t = null;
  123.       }
  124.     }
  125.  
  126.     // Say bye and flush output
  127.     out.println( "Bye!" );
  128.     out.flush();
  129.  
  130.       } catch( Exception e ) {
  131.     System.out.println( "reading: " + e );
  132.       }
  133.  
  134.       // Close socket
  135.       try {
  136.     out.close();
  137.       } catch ( Exception e ) {
  138.     System.out.println( "close: " + e );
  139.       }
  140.     }
  141.   } 
  142. }
  143.  
  144. // Wrapper for Listen class
  145. public class Server extends Applet {
  146.  
  147.   public void init( ) {
  148.     java.applet.AppletContext a = getAppletContext();
  149.  
  150.     System.out.println( "init called" );
  151.  
  152.     System.out.println( "app context: " + a );
  153.   }
  154.  
  155.   public static void main( String args[] ) {
  156.  
  157.     Listen l = new Listen();    // Create a Listen object
  158.     l.start();            // Start server thread running 
  159.  
  160.     System.out.println( "Thread started" );
  161.     System.out.println( "Waiting for l to die" );
  162.  
  163.     // Join with server thread
  164.     try {
  165.       l.join();
  166.     } catch ( InterruptedException e ) {
  167.       System.out.println( "join interrupted: " + e );
  168.       System.exit( 1 );        // Exit with error
  169.     }
  170.  
  171.     l = null;
  172.  
  173.     System.exit( 0 );        // Exit gracefully
  174.   }
  175.  
  176. }
  177.