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