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