%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
examples.ntp.TimeClient |
|
|
1 | package examples.ntp; |
|
2 | ||
3 | /* |
|
4 | * Copyright 2001-2005 The Apache Software Foundation |
|
5 | * |
|
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
7 | * you may not use this file except in compliance with the License. |
|
8 | * You may obtain a copy of the License at |
|
9 | * |
|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
11 | * |
|
12 | * Unless required by applicable law or agreed to in writing, software |
|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15 | * See the License for the specific language governing permissions and |
|
16 | * limitations under the License. |
|
17 | */ |
|
18 | ||
19 | import java.io.IOException; |
|
20 | import java.net.InetAddress; |
|
21 | import org.apache.commons.net.TimeTCPClient; |
|
22 | import org.apache.commons.net.TimeUDPClient; |
|
23 | ||
24 | /*** |
|
25 | * This is an example program demonstrating how to use the TimeTCPClient |
|
26 | * and TimeUDPClient classes. |
|
27 | * This program connects to the default time service port of a |
|
28 | * specified server, retrieves the time, and prints it to standard output. |
|
29 | * See <A HREF="ftp://ftp.rfc-editor.org/in-notes/rfc868.txt"> the spec </A> |
|
30 | * for details. The default is to use the TCP port. Use the -udp flag to |
|
31 | * use the UDP port. |
|
32 | * <p> |
|
33 | * Usage: TimeClient [-udp] <hostname> |
|
34 | * <p> |
|
35 | ***/ |
|
36 | 0 | public final class TimeClient |
37 | { |
|
38 | ||
39 | public static final void timeTCP(String host) throws IOException |
|
40 | { |
|
41 | 0 | TimeTCPClient client = new TimeTCPClient(); |
42 | try { |
|
43 | // We want to timeout if a response takes longer than 60 seconds |
|
44 | 0 | client.setDefaultTimeout(60000); |
45 | 0 | client.connect(host); |
46 | 0 | System.out.println(client.getDate()); |
47 | } finally { |
|
48 | 0 | client.disconnect(); |
49 | 0 | } |
50 | 0 | } |
51 | ||
52 | public static final void timeUDP(String host) throws IOException |
|
53 | { |
|
54 | 0 | TimeUDPClient client = new TimeUDPClient(); |
55 | ||
56 | // We want to timeout if a response takes longer than 60 seconds |
|
57 | 0 | client.setDefaultTimeout(60000); |
58 | 0 | client.open(); |
59 | 0 | System.out.println(client.getDate(InetAddress.getByName(host))); |
60 | 0 | client.close(); |
61 | 0 | } |
62 | ||
63 | public static final void main(String[] args) |
|
64 | { |
|
65 | ||
66 | 0 | if (args.length == 1) |
67 | { |
|
68 | try |
|
69 | { |
|
70 | 0 | timeTCP(args[0]); |
71 | } |
|
72 | 0 | catch (IOException e) |
73 | { |
|
74 | 0 | e.printStackTrace(); |
75 | 0 | System.exit(1); |
76 | 0 | } |
77 | } |
|
78 | 0 | else if (args.length == 2 && args[0].equals("-udp")) |
79 | { |
|
80 | try |
|
81 | { |
|
82 | 0 | timeUDP(args[1]); |
83 | } |
|
84 | 0 | catch (IOException e) |
85 | { |
|
86 | 0 | e.printStackTrace(); |
87 | 0 | System.exit(1); |
88 | 0 | } |
89 | } |
|
90 | else |
|
91 | { |
|
92 | 0 | System.err.println("Usage: TimeClient [-udp] <hostname>"); |
93 | 0 | System.exit(1); |
94 | } |
|
95 | ||
96 | 0 | } |
97 | ||
98 | } |
|
99 |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |