1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
32
33
34
35
36
37
38
39
40
41
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
54 client.setDefaultTimeout(60000);
55 client.connect(host);
56 chargenInput =
57 new BufferedReader(new InputStreamReader(client.getInputStream()));
58
59
60
61
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
84
85 client.setSoTimeout(5000);
86
87 while (packets-- > 0)
88 {
89 client.send(address);
90
91 try
92 {
93 data = client.receive();
94 }
95
96
97
98
99 catch (SocketException e)
100 {
101
102 System.err.println("SocketException: Timed out and dropped packet");
103 continue;
104 }
105 catch (InterruptedIOException e)
106 {
107
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