rdate.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net.examples.unix;

  18. import java.io.IOException;
  19. import java.net.InetAddress;
  20. import java.time.Duration;

  21. import org.apache.commons.net.time.TimeTCPClient;
  22. import org.apache.commons.net.time.TimeUDPClient;

  23. /**
  24.  * This is an example program demonstrating how to use the TimeTCPClient and TimeUDPClient classes. It's very similar to the simple UNIX rdate command. This
  25.  * program connects to the default time service port of a specified server, retrieves the time, and prints it to standard output. The default is to use the TCP
  26.  * port. Use the -udp flag to use the UDP port. You can test this program by using the NIST time server at 132.163.135.130 (warning: the IP address may change).
  27.  * <p>
  28.  * Usage: rdate [-udp] <hostname>
  29.  */
  30. public final class rdate {

  31.     public static void main(final String[] args) {

  32.         if (args.length == 1) {
  33.             try {
  34.                 timeTCP(args[0]);
  35.             } catch (final IOException e) {
  36.                 e.printStackTrace();
  37.                 System.exit(1);
  38.             }
  39.         } else if (args.length == 2 && args[0].equals("-udp")) {
  40.             try {
  41.                 timeUDP(args[1]);
  42.             } catch (final IOException e) {
  43.                 e.printStackTrace();
  44.                 System.exit(1);
  45.             }
  46.         } else {
  47.             System.err.println("Usage: rdate [-udp] <hostname>");
  48.             System.exit(1);
  49.         }

  50.     }

  51.     public static void timeTCP(final String host) throws IOException {
  52.         final TimeTCPClient client = new TimeTCPClient();

  53.         // We want to timeout if a response takes longer than 60 seconds
  54.         client.setDefaultTimeout(60000);
  55.         client.connect(host);
  56.         System.out.println(client.getDate().toString());
  57.         client.disconnect();
  58.     }

  59.     public static void timeUDP(final String host) throws IOException {
  60.         try (TimeUDPClient client = new TimeUDPClient()) {
  61.             // We want to timeout if a response takes longer than 60 seconds
  62.             client.setDefaultTimeout(Duration.ofSeconds(60));
  63.             client.open();
  64.             System.out.println(client.getDate(InetAddress.getByName(host)).toString());
  65.         }
  66.     }

  67. }