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