View Javadoc
1   package examples.ntp;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  
21  import java.io.IOException;
22  import java.net.InetAddress;
23  
24  import org.apache.commons.net.time.TimeTCPClient;
25  import org.apache.commons.net.time.TimeUDPClient;
26  
27  /***
28   * This is an example program demonstrating how to use the TimeTCPClient
29   * and TimeUDPClient classes.
30   * This program connects to the default time service port of a
31   * specified server, retrieves the time, and prints it to standard output.
32   * See <A HREF="ftp://ftp.rfc-editor.org/in-notes/rfc868.txt"> the spec </A>
33   * for details.  The default is to use the TCP port.  Use the -udp flag to
34   * use the UDP port.
35   * <p>
36   * Usage: TimeClient [-udp] <hostname>
37   ***/
38  public final class TimeClient
39  {
40  
41      public static final void timeTCP(String host) throws IOException
42      {
43          TimeTCPClient client = new TimeTCPClient();
44      try {
45            // We want to timeout if a response takes longer than 60 seconds
46            client.setDefaultTimeout(60000);
47        client.connect(host);
48            System.out.println(client.getDate());
49      } finally {
50            client.disconnect();
51      }
52      }
53  
54      public static final void timeUDP(String host) throws IOException
55      {
56          TimeUDPClient client = new TimeUDPClient();
57  
58          // We want to timeout if a response takes longer than 60 seconds
59          client.setDefaultTimeout(60000);
60          client.open();
61          System.out.println(client.getDate(InetAddress.getByName(host)));
62          client.close();
63      }
64  
65      public static void main(String[] args)
66      {
67  
68          if (args.length == 1)
69          {
70              try
71              {
72                  timeTCP(args[0]);
73              }
74              catch (IOException e)
75              {
76                  e.printStackTrace();
77                  System.exit(1);
78              }
79          }
80          else if (args.length == 2 && args[0].equals("-udp"))
81          {
82              try
83              {
84                  timeUDP(args[1]);
85              }
86              catch (IOException e)
87              {
88                  e.printStackTrace();
89                  System.exit(1);
90              }
91          }
92          else
93          {
94              System.err.println("Usage: TimeClient [-udp] <hostname>");
95              System.exit(1);
96          }
97  
98      }
99  
100 }
101