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