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

public class DayTimeClient {
    
    public static void main(String args[]) {
        Socket theSocket;
        String hostname = "localhost";
        int port = 12345;
        InputStream is;
        
        if (args.length == 2 ) {
            hostname = args[0];
            port = Integer.parseInt(args[1]);
        }
        else 
            if (args.length == 1) {
                port = Integer.parseInt(args[0]);
            }
            else { 
                System.out.println("USAGE: java DayTimeClient [host] [port]");
                System.exit(0);
            }

        try {

            theSocket = new Socket(hostname, port);
            is = theSocket.getInputStream();
            BufferedReader buf = new BufferedReader(new InputStreamReader(is));
            String time = buf.readLine();
            System.out.println("If I tell you the time, will you stop bothering me?");
            System.out.println("Ok, the time is: " + time);
            System.out.println("Now leave me alone!");
        }
        catch(UnknownHostException e) {
            System.out.println("Unknown Host: " + e);
        }
        catch( IOException e) {
            System.out.println("Got IOException: " + e);
        }
    }
}
