1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.net.examples.unix;
19
20 import java.io.IOException;
21 import java.net.InetAddress;
22 import java.time.Duration;
23
24 import org.apache.commons.net.daytime.DaytimeTCPClient;
25 import org.apache.commons.net.daytime.DaytimeUDPClient;
26
27
28
29
30
31
32
33 public final class daytime {
34
35 public static void daytimeTCP(final String host) throws IOException {
36 final DaytimeTCPClient client = new DaytimeTCPClient();
37
38
39 client.setDefaultTimeout(60000);
40 client.connect(host);
41 System.out.println(client.getTime().trim());
42 client.disconnect();
43 }
44
45 public static void daytimeUDP(final String host) throws IOException {
46 try (DaytimeUDPClient client = new DaytimeUDPClient()) {
47
48
49 client.setDefaultTimeout(Duration.ofSeconds(60));
50 client.open();
51 System.out.println(client.getTime(InetAddress.getByName(host)).trim());
52 }
53 }
54
55 public static void main(final String[] args) {
56
57 if (args.length == 1) {
58 try {
59 daytimeTCP(args[0]);
60 } catch (final IOException e) {
61 e.printStackTrace();
62 System.exit(1);
63 }
64 } else if (args.length == 2 && args[0].equals("-udp")) {
65 try {
66 daytimeUDP(args[1]);
67 } catch (final IOException e) {
68 e.printStackTrace();
69 System.exit(1);
70 }
71 } else {
72 System.err.println("Usage: daytime [-udp] <hostname>");
73 System.exit(1);
74 }
75
76 }
77
78 }