
/*
    This program opens a connection to a computer specified
    as the first command-line argument.  The connection is made to
    the port specified by LISTENING_PORT.  The program reads one
    line of text from the connection and then closes the
    connection.  It displayes the text that it read on
    standard output.  This program is meant to be used with
    the server program, DataServ, which sends the current
    date and time on the computer where the server is running.
    (This program uses the non-standard class, TextReader.)
*/

import java.net.*;
import java.io.*;

public class DateClient {

   static final int LISTENING_PORT = 32007;

   public static void main(String[] args) {
   
      String computer;     // Name of the computer to connect to.
      Socket connection;   // A socket for communicating with
                           //                that computer.
      Reader incoming;     // Stream for reading data from
                           //                the connection.
       
      /* Get computer name from command line. */
      
      if (args.length > 0)
         computer = args[0];
      else {
            // No computer name was given.  Print a message and exit.
         System.out.println("Usage:  java DateClient <server>");
         return;
      }
      
      /* Make the connection, then read and display a line of text. */
      
      try {
         connection = new Socket( computer, LISTENING_PORT );
         incoming = new InputStreamReader( connection.getInputStream() );
         while (true) {
            int ch = incoming.read(); 
            if (ch == -1 || ch == '\n' || ch == '\r')
               break;
            System.out.print( (char)ch );
         }
         System.out.println();
         incoming.close();
      }
      catch (Exception e) {
         TextIO.putln("Error:  " + e);
      }
      
   }  // end main()
   

} //end class DateClient

