import java.io.*;
import java.net.*;

public class TimeClient {

	// The Time protocol defined in RFC 867 returns the server's time

	private static final int TIME_PORT = 13;

    public static void main(String[] args)  throws Exception{
		String hostName = null;
		try {
			hostName = args[0];
			System.out.println(getServerTime(hostName));
		} catch (UnknownHostException e) {
			System.err.println("Don't know about host" + hostName);
			System.exit(1);
		} catch (IOException e) {
			System.err.println("Couldn't get I/O for "
								   + "the connection to:" + hostName);
			System.exit(1);
		} catch (ArrayIndexOutOfBoundsException aiobe) {
			System.err.println("wrong usage: enter hostname");
			System.exit(1);
		}
	}

	// returns the date from the specified host name using the time protocol
	private static String getServerTime(String hostName)
		throws IOException, UnknownHostException {
			Socket timeSocket = new Socket(hostName, TIME_PORT);
			BufferedReader reader = new BufferedReader(
                    new InputStreamReader(timeSocket.getInputStream()));
			String serverTime = reader.readLine();
			timeSocket.close();
			return serverTime;
	}
}

