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  
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   */
42  public final class chargen
43  {
44  
45      public static void chargenTCP(final String host) throws IOException
46      {
47          int lines = 100;
48          String line;
49          final CharGenTCPClientharGenTCPClient.html#CharGenTCPClient">CharGenTCPClient client = new CharGenTCPClient();
50  
51          // We want to timeout if a response takes longer than 60 seconds
52          client.setDefaultTimeout(60000);
53          client.connect(host);
54          try (final BufferedReader chargenInput = new BufferedReader(new InputStreamReader(client.getInputStream()))) {
55  
56              // We assume the chargen service outputs lines, but it really doesn't
57              // have to, so this code might actually not work if no newlines are
58              // present.
59              while (lines-- > 0) {
60                  if ((line = chargenInput.readLine()) == null) {
61                      break;
62                  }
63                  System.out.println(line);
64              }
65          }
66          client.disconnect();
67      }
68  
69      public static void chargenUDP(final String host) throws IOException
70      {
71          int packets = 50;
72          byte[] data;
73          final InetAddress address;
74          final CharGenUDPClient client;
75  
76          address = InetAddress.getByName(host);
77          client = new CharGenUDPClient();
78  
79          client.open();
80          // If we don't receive a return packet within 5 seconds, assume
81          // the packet is lost.
82          client.setSoTimeout(5000);
83  
84          while (packets-- > 0)
85          {
86              client.send(address);
87  
88              try
89              {
90                  data = client.receive();
91              }
92              // Here we catch both SocketException and InterruptedIOException,
93              // because even though the JDK 1.1 docs claim that
94              // InterruptedIOException is thrown on a timeout, it seems
95              // SocketException is also thrown.
96              catch (final SocketException e)
97              {
98                  // We timed out and assume the packet is lost.
99                  System.err.println("SocketException: Timed out and dropped packet");
100                 continue;
101             }
102             catch (final InterruptedIOException e)
103             {
104                 // We timed out and assume the packet is lost.
105                 System.err.println(
106                     "InterruptedIOException: Timed out and dropped packet");
107                 continue;
108             }
109             System.out.write(data);
110             System.out.flush();
111         }
112 
113         client.close();
114     }
115 
116 
117     public static void main(final String[] args)
118     {
119 
120         if (args.length == 1)
121         {
122             try
123             {
124                 chargenTCP(args[0]);
125             }
126             catch (final IOException e)
127             {
128                 e.printStackTrace();
129                 System.exit(1);
130             }
131         }
132         else if (args.length == 2 && args[0].equals("-udp"))
133         {
134             try
135             {
136                 chargenUDP(args[1]);
137             }
138             catch (final IOException e)
139             {
140                 e.printStackTrace();
141                 System.exit(1);
142             }
143         }
144         else
145         {
146             System.err.println("Usage: chargen [-udp] <hostname>");
147             System.exit(1);
148         }
149 
150     }
151 
152 }
153