chargen.java

  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. package org.apache.commons.net.examples.unix;

  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStreamReader;
  21. import java.io.InterruptedIOException;
  22. import java.net.InetAddress;
  23. import java.net.SocketException;
  24. import java.nio.charset.Charset;
  25. import java.time.Duration;

  26. import org.apache.commons.net.chargen.CharGenTCPClient;
  27. import org.apache.commons.net.chargen.CharGenUDPClient;

  28. /**
  29.  * This is an example program demonstrating how to use the CharGenTCPClient and CharGenUDPClient classes. This program connects to the default chargen service
  30.  * 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
  31.  * 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.
  32.  * <p>
  33.  * Usage: chargen [-udp] <hostname>
  34.  */
  35. public final class chargen {

  36.     public static void chargenTCP(final String host) throws IOException {
  37.         int lines = 100;
  38.         String line;
  39.         final CharGenTCPClient client = new CharGenTCPClient();

  40.         // We want to timeout if a response takes longer than 60 seconds
  41.         client.setDefaultTimeout(60000);
  42.         client.connect(host);
  43.         try (final BufferedReader chargenInput = new BufferedReader(new InputStreamReader(client.getInputStream(), Charset.defaultCharset()))) {

  44.             // We assume the chargen service outputs lines, but it really doesn't
  45.             // have to, so this code might actually not work if no newlines are
  46.             // present.
  47.             while (lines-- > 0) {
  48.                 if ((line = chargenInput.readLine()) == null) {
  49.                     break;
  50.                 }
  51.                 System.out.println(line);
  52.             }
  53.         }
  54.         client.disconnect();
  55.     }

  56.     public static void chargenUDP(final String host) throws IOException {
  57.         int packets = 50;
  58.         byte[] data;
  59.         final InetAddress address;

  60.         address = InetAddress.getByName(host);
  61.         try (CharGenUDPClient client = new CharGenUDPClient()) {

  62.             client.open();
  63.             // If we don't receive a return packet within 5 seconds, assume
  64.             // the packet is lost.
  65.             client.setSoTimeout(Duration.ofSeconds(5));

  66.             while (packets-- > 0) {
  67.                 client.send(address);

  68.                 try {
  69.                     data = client.receive();
  70.                 }
  71.                 // Here we catch both SocketException and InterruptedIOException,
  72.                 // because even though the JDK 1.1 docs claim that
  73.                 // InterruptedIOException is thrown on a timeout, it seems
  74.                 // SocketException is also thrown.
  75.                 catch (final SocketException e) {
  76.                     // We timed out and assume the packet is lost.
  77.                     System.err.println("SocketException: Timed out and dropped packet");
  78.                     continue;
  79.                 } catch (final InterruptedIOException e) {
  80.                     // We timed out and assume the packet is lost.
  81.                     System.err.println("InterruptedIOException: Timed out and dropped packet");
  82.                     continue;
  83.                 }
  84.                 System.out.write(data);
  85.                 System.out.flush();
  86.             }

  87.         }
  88.     }

  89.     public static void main(final String[] args) {

  90.         if (args.length == 1) {
  91.             try {
  92.                 chargenTCP(args[0]);
  93.             } catch (final IOException e) {
  94.                 e.printStackTrace();
  95.                 System.exit(1);
  96.             }
  97.         } else if (args.length == 2 && args[0].equals("-udp")) {
  98.             try {
  99.                 chargenUDP(args[1]);
  100.             } catch (final IOException e) {
  101.                 e.printStackTrace();
  102.                 System.exit(1);
  103.             }
  104.         } else {
  105.             System.err.println("Usage: chargen [-udp] <hostname>");
  106.             System.exit(1);
  107.         }

  108.     }

  109. }