daytime.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.daytime.DaytimeTCPClient;
  22. import org.apache.commons.net.daytime.DaytimeUDPClient;

  23. /**
  24.  * This is an example program demonstrating how to use the DaytimeTCP and DaytimeUDP classes. This program connects to the default daytime service port of a
  25.  * specified server, retrieves the daytime, and prints it to standard output. The default is to use the TCP port. Use the -udp flag to use the UDP port.
  26.  * <p>
  27.  * Usage: daytime [-udp] <hostname>
  28.  */
  29. public final class daytime {

  30.     public static void daytimeTCP(final String host) throws IOException {
  31.         final DaytimeTCPClient client = new DaytimeTCPClient();

  32.         // We want to timeout if a response takes longer than 60 seconds
  33.         client.setDefaultTimeout(60000);
  34.         client.connect(host);
  35.         System.out.println(client.getTime().trim());
  36.         client.disconnect();
  37.     }

  38.     public static void daytimeUDP(final String host) throws IOException {
  39.         try (DaytimeUDPClient client = new DaytimeUDPClient()) {

  40.             // We want to timeout if a response takes longer than 60 seconds
  41.             client.setDefaultTimeout(Duration.ofSeconds(60));
  42.             client.open();
  43.             System.out.println(client.getTime(InetAddress.getByName(host)).trim());
  44.         }
  45.     }

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

  47.         if (args.length == 1) {
  48.             try {
  49.                 daytimeTCP(args[0]);
  50.             } catch (final IOException e) {
  51.                 e.printStackTrace();
  52.                 System.exit(1);
  53.             }
  54.         } else if (args.length == 2 && args[0].equals("-udp")) {
  55.             try {
  56.                 daytimeUDP(args[1]);
  57.             } catch (final IOException e) {
  58.                 e.printStackTrace();
  59.                 System.exit(1);
  60.             }
  61.         } else {
  62.             System.err.println("Usage: daytime [-udp] <hostname>");
  63.             System.exit(1);
  64.         }

  65.     }

  66. }