001package examples.ntp;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements.  See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License.  You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020
021import java.io.IOException;
022import java.net.InetAddress;
023
024import org.apache.commons.net.time.TimeTCPClient;
025import org.apache.commons.net.time.TimeUDPClient;
026
027/***
028 * This is an example program demonstrating how to use the TimeTCPClient
029 * and TimeUDPClient classes.
030 * This program connects to the default time service port of a
031 * specified server, retrieves the time, and prints it to standard output.
032 * See <A HREF="ftp://ftp.rfc-editor.org/in-notes/rfc868.txt"> the spec </A>
033 * for details.  The default is to use the TCP port.  Use the -udp flag to
034 * use the UDP port.
035 * <p>
036 * Usage: TimeClient [-udp] <hostname>
037 ***/
038public final class TimeClient
039{
040
041    public static final void timeTCP(String host) throws IOException
042    {
043        TimeTCPClient client = new TimeTCPClient();
044    try {
045          // We want to timeout if a response takes longer than 60 seconds
046          client.setDefaultTimeout(60000);
047      client.connect(host);
048          System.out.println(client.getDate());
049    } finally {
050          client.disconnect();
051    }
052    }
053
054    public static final void timeUDP(String host) throws IOException
055    {
056        TimeUDPClient client = new TimeUDPClient();
057
058        // We want to timeout if a response takes longer than 60 seconds
059        client.setDefaultTimeout(60000);
060        client.open();
061        System.out.println(client.getDate(InetAddress.getByName(host)));
062        client.close();
063    }
064
065    public static void main(String[] args)
066    {
067
068        if (args.length == 1)
069        {
070            try
071            {
072                timeTCP(args[0]);
073            }
074            catch (IOException e)
075            {
076                e.printStackTrace();
077                System.exit(1);
078            }
079        }
080        else if (args.length == 2 && args[0].equals("-udp"))
081        {
082            try
083            {
084                timeUDP(args[1]);
085            }
086            catch (IOException e)
087            {
088                e.printStackTrace();
089                System.exit(1);
090            }
091        }
092        else
093        {
094            System.err.println("Usage: TimeClient [-udp] <hostname>");
095            System.exit(1);
096        }
097
098    }
099
100}
101