1 package examples;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Commons" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.io.BufferedReader;
58 import java.io.IOException;
59 import java.io.InputStreamReader;
60 import java.io.InterruptedIOException;
61 import java.net.InetAddress;
62 import java.net.SocketException;
63 import org.apache.commons.net.CharGenTCPClient;
64 import org.apache.commons.net.CharGenUDPClient;
65
66 /****
67 * This is an example program demonstrating how to use the CharGenTCPClient
68 * and CharGenUDPClient classes. This program connects to the default
69 * chargen service port of a specified server, then reads 100 lines from
70 * of generated output, writing each line to standard output, and then
71 * closes the connection. The UDP invocation of the program sends 50
72 * datagrams, printing the reply to each.
73 * The default is to use the TCP port. Use the -udp flag to use the UDP
74 * port.
75 * <p>
76 * Usage: chargen [-udp] <hostname>
77 * <p>
78 ***/
79 public final class chargen
80 {
81
82 public static final void chargenTCP(String host) throws IOException
83 {
84 int lines = 100;
85 String line;
86 CharGenTCPClient client = new CharGenTCPClient();
87 BufferedReader chargenInput;
88
89 // We want to timeout if a response takes longer than 60 seconds
90 client.setDefaultTimeout(60000);
91 client.connect(host);
92 chargenInput =
93 new BufferedReader(new InputStreamReader(client.getInputStream()));
94
95 // We assume the chargen service outputs lines, but it really doesn't
96 // have to, so this code might actually not work if no newlines are
97 // present.
98 while (lines-- > 0)
99 {
100 if ((line = chargenInput.readLine()) == null)
101 break;
102 System.out.println(line);
103 }
104
105 client.disconnect();
106 }
107
108 public static final void chargenUDP(String host) throws IOException
109 {
110 int packets = 50;
111 byte[] data;
112 InetAddress address;
113 CharGenUDPClient client;
114
115 address = InetAddress.getByName(host);
116 client = new CharGenUDPClient();
117
118 client.open();
119 // If we don't receive a return packet within 5 seconds, assume
120 // the packet is lost.
121 client.setSoTimeout(5000);
122
123 while (packets-- > 0)
124 {
125 client.send(address);
126
127 try
128 {
129 data = client.receive();
130 }
131 // Here we catch both SocketException and InterruptedIOException,
132 // because even though the JDK 1.1 docs claim that
133 // InterruptedIOException is thrown on a timeout, it seems
134 // SocketException is also thrown.
135 catch (SocketException e)
136 {
137 // We timed out and assume the packet is lost.
138 System.err.println("SocketException: Timed out and dropped packet");
139 continue;
140 }
141 catch (InterruptedIOException e)
142 {
143 // We timed out and assume the packet is lost.
144 System.err.println(
145 "InterruptedIOException: Timed out and dropped packet");
146 continue;
147 }
148 System.out.write(data);
149 System.out.flush();
150 }
151
152 client.close();
153 }
154
155
156 public static final void main(String[] args)
157 {
158
159 if (args.length == 1)
160 {
161 try
162 {
163 chargenTCP(args[0]);
164 }
165 catch (IOException e)
166 {
167 e.printStackTrace();
168 System.exit(1);
169 }
170 }
171 else if (args.length == 2 && args[0].equals("-udp"))
172 {
173 try
174 {
175 chargenUDP(args[1]);
176 }
177 catch (IOException e)
178 {
179 e.printStackTrace();
180 System.exit(1);
181 }
182 }
183 else
184 {
185 System.err.println("Usage: chargen [-udp] <hostname>");
186 System.exit(1);
187 }
188
189 }
190
191 }
192
This page was automatically generated by Maven