001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package examples;
019
020 import java.io.BufferedReader;
021 import java.io.IOException;
022 import java.io.InputStreamReader;
023 import java.io.InterruptedIOException;
024 import java.net.InetAddress;
025 import java.net.SocketException;
026
027 import org.apache.commons.net.chargen.CharGenTCPClient;
028 import org.apache.commons.net.chargen.CharGenUDPClient;
029
030 /***
031 * This is an example program demonstrating how to use the CharGenTCPClient
032 * and CharGenUDPClient classes. This program connects to the default
033 * chargen service port of a specified server, then reads 100 lines from
034 * of generated output, writing each line to standard output, and then
035 * closes the connection. The UDP invocation of the program sends 50
036 * datagrams, printing the reply to each.
037 * The default is to use the TCP port. Use the -udp flag to use the UDP
038 * port.
039 * <p>
040 * Usage: chargen [-udp] <hostname>
041 * <p>
042 ***/
043 public final class chargen
044 {
045
046 public static final void chargenTCP(String host) throws IOException
047 {
048 int lines = 100;
049 String line;
050 CharGenTCPClient client = new CharGenTCPClient();
051 BufferedReader chargenInput;
052
053 // We want to timeout if a response takes longer than 60 seconds
054 client.setDefaultTimeout(60000);
055 client.connect(host);
056 chargenInput =
057 new BufferedReader(new InputStreamReader(client.getInputStream()));
058
059 // We assume the chargen service outputs lines, but it really doesn't
060 // have to, so this code might actually not work if no newlines are
061 // present.
062 while (lines-- > 0)
063 {
064 if ((line = chargenInput.readLine()) == null)
065 break;
066 System.out.println(line);
067 }
068
069 client.disconnect();
070 }
071
072 public static final void chargenUDP(String host) throws IOException
073 {
074 int packets = 50;
075 byte[] data;
076 InetAddress address;
077 CharGenUDPClient client;
078
079 address = InetAddress.getByName(host);
080 client = new CharGenUDPClient();
081
082 client.open();
083 // If we don't receive a return packet within 5 seconds, assume
084 // the packet is lost.
085 client.setSoTimeout(5000);
086
087 while (packets-- > 0)
088 {
089 client.send(address);
090
091 try
092 {
093 data = client.receive();
094 }
095 // Here we catch both SocketException and InterruptedIOException,
096 // because even though the JDK 1.1 docs claim that
097 // InterruptedIOException is thrown on a timeout, it seems
098 // SocketException is also thrown.
099 catch (SocketException e)
100 {
101 // We timed out and assume the packet is lost.
102 System.err.println("SocketException: Timed out and dropped packet");
103 continue;
104 }
105 catch (InterruptedIOException e)
106 {
107 // We timed out and assume the packet is lost.
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