FileDocCategorySizeDatePackage
Chapters3and4.javaAPI DocExample3239Thu Oct 04 11:59:12 BST 2001None

Chapters3and4.java

// File: Chapters3and4.java
// T Balls : March 2001

// Contains two classes
//	 TimeServer - answer to Ex 3.5 # 4
//	 TimeServer2 - answer to Ex 4.5 # 4

/* 
 * Modification of example EchoServer program to 
 * emulate Unix time-of-day service
 */
   
import java.net.*;
import java.io.*;
/**/ import java.util.Date;


class TimeServer
{  public static void main( String[] args )
   {  new TimeServer( 44444 ).run();
   }
   
   public TimeServer( int port )
   {  this.port = port;
   }

   private int port;
   private ServerSocket sSock;
   private Socket sock;
/**/ //  private InputStream ins;
/**/   private PrintWriter outs;
   
   public void run()
   {  try
      {  sSock = new ServerSocket( port );
      }
      catch( IOException ioe )
      {  System.out.println( "Couldn't listen on port " + port );
         System.exit( -1 );
      }
      try
      {  sock = sSock.accept();
      }
      catch( IOException ioe )
      {  System.out.println( "Accept failed" );
         System.exit( -1 );
      }
      try
/**/  {  // ins = sock.getInputStream(); // only need output
         outs = new PrintWriter( sock.getOutputStream() );
      }
      catch( IOException ioe )
      {  System.out.println( "Failed to get I/O streams" );
         System.exit( -1 );
      }

      try
/**/  {  //int inChar = ins.read();
/*
         while( inChar != -1 )
         {  outs.write( inChar );
            outs.flush();
            System.out.print( (char)inChar );
            inChar = ins.read();
         }
*/
/**/     Date d = new Date();
/**/     outs.println( d.toString() );
/**/     outs.flush();         
         sock.close();
      }
      catch( IOException ioe )
      {  System.out.println( "Read/write failed" );
         System.exit( -1 );
      }
   }
}

/**
 * This class will continue to server date/time
 * to a sequesnce of clients
 */
 
class TimeServer2
{  public static void main( String[] args )
   {  new TimeServer2( 44444 ).run();
   }
   
   public TimeServer2( int port )
   {  this.port = port;
   }

   private int port;
   private ServerSocket sSock;
   private Socket sock;
   
   public void run()
   {  try
      {  sSock = new ServerSocket( port );
      }
      catch( IOException ioe )
      {  System.out.println( "Couldn't listen on port " + port );
         System.exit( -1 );
      }
      while( true )
      {  try
         {  sock = sSock.accept();
         }
         catch( IOException ioe )
         {  System.out.println( "Accept failed" );
            System.exit( -1 );
         }
         handleConnection( sock );
      }
   }
   
   private void handleConnection( Socket s )
   {  PrintWriter outs = null;
      try
      {  outs = new PrintWriter( s.getOutputStream(), true );
         // second parameter creates a stream that automatically flushes
         // each time an end of line symbol is sent
      }
      catch( IOException ioe )
      {  System.out.println( "Failed to get I/O streams" );
         System.exit( -1 );
      }

      try
      {  Date d = new Date();
         outs.println( d.toString() );         
         s.close();
      }
      catch( IOException ioe )
      {  System.out.println( "Read/write failed" );
         System.exit( -1 );
      }
   }
}