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