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