001    package 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     
021    import java.io.IOException;
022    import java.net.InetAddress;
023    
024    import org.apache.commons.net.time.TimeTCPClient;
025    import 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     * <p>
038     ***/
039    public final class TimeClient
040    {
041    
042        public static final void timeTCP(String host) throws IOException
043        {
044            TimeTCPClient client = new TimeTCPClient();
045        try {
046              // We want to timeout if a response takes longer than 60 seconds
047              client.setDefaultTimeout(60000);
048          client.connect(host);
049              System.out.println(client.getDate());
050        } finally {
051              client.disconnect();
052        }
053        }
054    
055        public static final void timeUDP(String host) throws IOException
056        {
057            TimeUDPClient client = new TimeUDPClient();
058    
059            // We want to timeout if a response takes longer than 60 seconds
060            client.setDefaultTimeout(60000);
061            client.open();
062            System.out.println(client.getDate(InetAddress.getByName(host)));
063            client.close();
064        }
065    
066        public static final void main(String[] args)
067        {
068    
069            if (args.length == 1)
070            {
071                try
072                {
073                    timeTCP(args[0]);
074                }
075                catch (IOException e)
076                {
077                    e.printStackTrace();
078                    System.exit(1);
079                }
080            }
081            else if (args.length == 2 && args[0].equals("-udp"))
082            {
083                try
084                {
085                    timeUDP(args[1]);
086                }
087                catch (IOException e)
088                {
089                    e.printStackTrace();
090                    System.exit(1);
091                }
092            }
093            else
094            {
095                System.err.println("Usage: TimeClient [-udp] <hostname>");
096                System.exit(1);
097            }
098    
099        }
100    
101    }
102