1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package examples;
19
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.InputStreamReader;
23 import java.io.InterruptedIOException;
24 import java.net.InetAddress;
25 import java.net.SocketException;
26
27 import org.apache.commons.net.chargen.CharGenTCPClient;
28 import org.apache.commons.net.chargen.CharGenUDPClient;
29
30 /***
31 * This is an example program demonstrating how to use the CharGenTCPClient
32 * and CharGenUDPClient classes. This program connects to the default
33 * chargen service port of a specified server, then reads 100 lines from
34 * of generated output, writing each line to standard output, and then
35 * closes the connection. The UDP invocation of the program sends 50
36 * datagrams, printing the reply to each.
37 * The default is to use the TCP port. Use the -udp flag to use the UDP
38 * port.
39 * <p>
40 * Usage: chargen [-udp] <hostname>
41 * <p>
42 ***/
43 public final class chargen
44 {
45
46 public static final void chargenTCP(String host) throws IOException
47 {
48 int lines = 100;
49 String line;
50 CharGenTCPClient client = new CharGenTCPClient();
51 BufferedReader chargenInput;
52
53 // We want to timeout if a response takes longer than 60 seconds
54 client.setDefaultTimeout(60000);
55 client.connect(host);
56 chargenInput =
57 new BufferedReader(new InputStreamReader(client.getInputStream()));
58
59 // We assume the chargen service outputs lines, but it really doesn't
60 // have to, so this code might actually not work if no newlines are
61 // present.
62 while (lines-- > 0)
63 {
64 if ((line = chargenInput.readLine()) == null)
65 break;
66 System.out.println(line);
67 }
68
69 client.disconnect();
70 }
71
72 public static final void chargenUDP(String host) throws IOException
73 {
74 int packets = 50;
75 byte[] data;
76 InetAddress address;
77 CharGenUDPClient client;
78
79 address = InetAddress.getByName(host);
80 client = new CharGenUDPClient();
81
82 client.open();
83 // If we don't receive a return packet within 5 seconds, assume
84 // the packet is lost.
85 client.setSoTimeout(5000);
86
87 while (packets-- > 0)
88 {
89 client.send(address);
90
91 try
92 {
93 data = client.receive();
94 }
95 // Here we catch both SocketException and InterruptedIOException,
96 // because even though the JDK 1.1 docs claim that
97 // InterruptedIOException is thrown on a timeout, it seems
98 // SocketException is also thrown.
99 catch (SocketException e)
100 {
101 // We timed out and assume the packet is lost.
102 System.err.println("SocketException: Timed out and dropped packet");
103 continue;
104 }
105 catch (InterruptedIOException e)
106 {
107 // We timed out and assume the packet is lost.
108 System.err.println(
109 "InterruptedIOException: Timed out and dropped packet");
110 continue;
111 }
112 System.out.write(data);
113 System.out.flush();
114 }
115
116 client.close();
117 }
118
119
120 public static final void main(String[] args)
121 {
122
123 if (args.length == 1)
124 {
125 try
126 {
127 chargenTCP(args[0]);
128 }
129 catch (IOException e)
130 {
131 e.printStackTrace();
132 System.exit(1);
133 }
134 }
135 else if (args.length == 2 && args[0].equals("-udp"))
136 {
137 try
138 {
139 chargenUDP(args[1]);
140 }
141 catch (IOException e)
142 {
143 e.printStackTrace();
144 System.exit(1);
145 }
146 }
147 else
148 {
149 System.err.println("Usage: chargen [-udp] <hostname>");
150 System.exit(1);
151 }
152
153 }
154
155 }
156