/*
 * Example from p. 506 of Java 1.1 Developer's Handbook.
 *
 * Modifications by BF Caviness 31 July 1997
 */

import java.io.*;	// for BufferedWriter, OpuputStreamWriter
import java.net.*;	// for Socket, ServerSocket
import java.util.*;	// for Date

public class DayServer2{
   // Instance variable
   ServerSocket ss;

   public static void main( String arg[] ) throws IOException{
       DayServer2 d = new DayServer2( );
       d.go();
   }

   public void go() throws IOException{

       Socket s = null;

      /*
       * Create a server socket and bind it to the specified local port 
       * number. A port number of 0 creates a socket on any free port. 
       * 
       * The maximum queue length for incoming connection indications (a 
       * request to connect) is set to the second parameter. If 
       * a connection indication arrives when the queue is full, the 
       * connection is refused. 
       */
       ss = new ServerSocket( DayClient2.DAYTIME_PORT, 5 );
       
       while( true ){
           // Listen for a connection to be made to this socket and accept
           // it. accept() blocks until a connection is made. 
           s = ss.accept();
           // Get an output stream for this socket and buffer it for eff.
           BufferedWriter out = new BufferedWriter(
                                new OutputStreamWriter(s.getOutputStream() ));
/*
 *         out.write( "Java Daytime Server: " + (new Date()) + "\n" );
 * Rather than use "\n" to terminate the line as is done in JDH,
 * BufferedWriter provides
 * a newLine() method that uses the platform's own notion of
 * line separator as defined by the system property line.separator.
 * Not all platforms use the newline character ('\n') to terminate lines.
 * Calling this method to terminate each output line is therefore preferred to
 * writing a newline character directly.
 */
           // Write a line to the output stream for reading by client.
           out.write( "Java Daytime Server: " + (new Date()) );
           out.newLine();

           out.close();
           s.close();
       }
   }
}
