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.unix;
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 }
67 System.out.println(line);
68 }
69
70 client.disconnect();
71 }
72
73 public static final void chargenUDP(String host) throws IOException
74 {
75 int packets = 50;
76 byte[] data;
77 InetAddress address;
78 CharGenUDPClient client;
79
80 address = InetAddress.getByName(host);
81 client = new CharGenUDPClient();
82
83 client.open();
84 // If we don't receive a return packet within 5 seconds, assume
85 // the packet is lost.
86 client.setSoTimeout(5000);
87
88 while (packets-- > 0)
89 {
90 client.send(address);
91
92 try
93 {
94 data = client.receive();
95 }
96 // Here we catch both SocketException and InterruptedIOException,
97 // because even though the JDK 1.1 docs claim that
98 // InterruptedIOException is thrown on a timeout, it seems
99 // SocketException is also thrown.
100 catch (SocketException e)
101 {
102 // We timed out and assume the packet is lost.
103 System.err.println("SocketException: Timed out and dropped packet");
104 continue;
105 }
106 catch (InterruptedIOException e)
107 {
108 // We timed out and assume the packet is lost.
109 System.err.println(
110 "InterruptedIOException: Timed out and dropped packet");
111 continue;
112 }
113 System.out.write(data);
114 System.out.flush();
115 }
116
117 client.close();
118 }
119
120
121 public static void main(String[] args)
122 {
123
124 if (args.length == 1)
125 {
126 try
127 {
128 chargenTCP(args[0]);
129 }
130 catch (IOException e)
131 {
132 e.printStackTrace();
133 System.exit(1);
134 }
135 }
136 else if (args.length == 2 && args[0].equals("-udp"))
137 {
138 try
139 {
140 chargenUDP(args[1]);
141 }
142 catch (IOException e)
143 {
144 e.printStackTrace();
145 System.exit(1);
146 }
147 }
148 else
149 {
150 System.err.println("Usage: chargen [-udp] <hostname>");
151 System.exit(1);
152 }
153
154 }
155
156 }
157