View Javadoc
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 org.apache.commons.net.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  import java.nio.charset.Charset;
27  import java.time.Duration;
28  
29  import org.apache.commons.net.chargen.CharGenTCPClient;
30  import org.apache.commons.net.chargen.CharGenUDPClient;
31  
32  /**
33   * This is an example program demonstrating how to use the CharGenTCPClient and CharGenUDPClient classes. This program connects to the default chargen service
34   * port of a specified server, then reads 100 lines from of generated output, writing each line to standard output, and then closes the connection. The UDP
35   * invocation of the program sends 50 datagrams, printing the reply to each. The default is to use the TCP port. Use the -udp flag to use the UDP port.
36   * <p>
37   * Usage: chargen [-udp] <hostname>
38   */
39  public final class chargen {
40  
41      public static void chargenTCP(final String host) throws IOException {
42          int lines = 100;
43          String line;
44          final CharGenTCPClient client = new CharGenTCPClient();
45  
46          // We want to timeout if a response takes longer than 60 seconds
47          client.setDefaultTimeout(60000);
48          client.connect(host);
49          try (final BufferedReader chargenInput = new BufferedReader(new InputStreamReader(client.getInputStream(), Charset.defaultCharset()))) {
50  
51              // We assume the chargen service outputs lines, but it really doesn't
52              // have to, so this code might actually not work if no newlines are
53              // present.
54              while (lines-- > 0) {
55                  if ((line = chargenInput.readLine()) == null) {
56                      break;
57                  }
58                  System.out.println(line);
59              }
60          }
61          client.disconnect();
62      }
63  
64      public static void chargenUDP(final String host) throws IOException {
65          int packets = 50;
66          byte[] data;
67          final InetAddress address;
68  
69          address = InetAddress.getByName(host);
70          try (CharGenUDPClient client = new CharGenUDPClient()) {
71  
72              client.open();
73              // If we don't receive a return packet within 5 seconds, assume
74              // the packet is lost.
75              client.setSoTimeout(Duration.ofSeconds(5));
76  
77              while (packets-- > 0) {
78                  client.send(address);
79  
80                  try {
81                      data = client.receive();
82                  }
83                  // Here we catch both SocketException and InterruptedIOException,
84                  // because even though the JDK 1.1 docs claim that
85                  // InterruptedIOException is thrown on a timeout, it seems
86                  // SocketException is also thrown.
87                  catch (final SocketException e) {
88                      // We timed out and assume the packet is lost.
89                      System.err.println("SocketException: Timed out and dropped packet");
90                      continue;
91                  } catch (final InterruptedIOException e) {
92                      // We timed out and assume the packet is lost.
93                      System.err.println("InterruptedIOException: Timed out and dropped packet");
94                      continue;
95                  }
96                  System.out.write(data);
97                  System.out.flush();
98              }
99  
100         }
101     }
102 
103     public static void main(final String[] args) {
104 
105         if (args.length == 1) {
106             try {
107                 chargenTCP(args[0]);
108             } catch (final IOException e) {
109                 e.printStackTrace();
110                 System.exit(1);
111             }
112         } else if (args.length == 2 && args[0].equals("-udp")) {
113             try {
114                 chargenUDP(args[1]);
115             } catch (final IOException e) {
116                 e.printStackTrace();
117                 System.exit(1);
118             }
119         } else {
120             System.err.println("Usage: chargen [-udp] <hostname>");
121             System.exit(1);
122         }
123 
124     }
125 
126 }