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
23 import org.apache.commons.net.time.TimeTCPClient;
24 import org.apache.commons.net.time.TimeUDPClient;
25
26
27
28
29
30
31
32
33
34
35
36
37 public final class rdate
38 {
39
40 public static void timeTCP(final String host) throws IOException
41 {
42 final TimeTCPClientimeTCPClient.html#TimeTCPClient">TimeTCPClient client = new TimeTCPClient();
43
44
45 client.setDefaultTimeout(60000);
46 client.connect(host);
47 System.out.println(client.getDate().toString());
48 client.disconnect();
49 }
50
51 public static void timeUDP(final String host) throws IOException
52 {
53 final TimeUDPClientimeUDPClient.html#TimeUDPClient">TimeUDPClient client = new TimeUDPClient();
54
55
56 client.setDefaultTimeout(60000);
57 client.open();
58 System.out.println(client.getDate(InetAddress.getByName(host)).toString());
59 client.close();
60 }
61
62
63 public static void main(final String[] args)
64 {
65
66 if (args.length == 1)
67 {
68 try
69 {
70 timeTCP(args[0]);
71 }
72 catch (final IOException e)
73 {
74 e.printStackTrace();
75 System.exit(1);
76 }
77 }
78 else if (args.length == 2 && args[0].equals("-udp"))
79 {
80 try
81 {
82 timeUDP(args[1]);
83 }
84 catch (final IOException e)
85 {
86 e.printStackTrace();
87 System.exit(1);
88 }
89 }
90 else
91 {
92 System.err.println("Usage: rdate [-udp] <hostname>");
93 System.exit(1);
94 }
95
96 }
97
98 }
99