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
018package org.apache.commons.net.examples.unix;
019
020import java.io.BufferedReader;
021import java.io.IOException;
022import java.io.InputStreamReader;
023import java.io.InterruptedIOException;
024import java.net.InetAddress;
025import java.net.SocketException;
026
027import org.apache.commons.net.chargen.CharGenTCPClient;
028import org.apache.commons.net.chargen.CharGenUDPClient;
029
030/**
031 * This is an example program demonstrating how to use the CharGenTCPClient and CharGenUDPClient classes. This program connects to the default chargen service
032 * 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
033 * 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.
034 * <p>
035 * Usage: chargen [-udp] <hostname>
036 */
037public final class chargen {
038
039    public static void chargenTCP(final String host) throws IOException {
040        int lines = 100;
041        String line;
042        final CharGenTCPClient client = new CharGenTCPClient();
043
044        // We want to timeout if a response takes longer than 60 seconds
045        client.setDefaultTimeout(60000);
046        client.connect(host);
047        try (final BufferedReader chargenInput = new BufferedReader(new InputStreamReader(client.getInputStream()))) {
048
049            // We assume the chargen service outputs lines, but it really doesn't
050            // have to, so this code might actually not work if no newlines are
051            // present.
052            while (lines-- > 0) {
053                if ((line = chargenInput.readLine()) == null) {
054                    break;
055                }
056                System.out.println(line);
057            }
058        }
059        client.disconnect();
060    }
061
062    public static void chargenUDP(final String host) throws IOException {
063        int packets = 50;
064        byte[] data;
065        final InetAddress address;
066        final CharGenUDPClient client;
067
068        address = InetAddress.getByName(host);
069        client = new CharGenUDPClient();
070
071        client.open();
072        // If we don't receive a return packet within 5 seconds, assume
073        // the packet is lost.
074        client.setSoTimeout(5000);
075
076        while (packets-- > 0) {
077            client.send(address);
078
079            try {
080                data = client.receive();
081            }
082            // Here we catch both SocketException and InterruptedIOException,
083            // because even though the JDK 1.1 docs claim that
084            // InterruptedIOException is thrown on a timeout, it seems
085            // SocketException is also thrown.
086            catch (final SocketException e) {
087                // We timed out and assume the packet is lost.
088                System.err.println("SocketException: Timed out and dropped packet");
089                continue;
090            } catch (final InterruptedIOException e) {
091                // We timed out and assume the packet is lost.
092                System.err.println("InterruptedIOException: Timed out and dropped packet");
093                continue;
094            }
095            System.out.write(data);
096            System.out.flush();
097        }
098
099        client.close();
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}