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

TimeServer

public class TimeServer extends Object

Fields Summary
private int
port
private ServerSocket
sSock
private Socket
sock
private PrintWriter
outs
Constructors Summary
public TimeServer(int port)

  this.port = port;
   
Methods Summary
public static voidmain(java.lang.String[] args)

  new TimeServer( 44444 ).run();
   
public voidrun()

  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 );
      }