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 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
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 ***/
042public final class chargen
043{
044
045    public static final void chargenTCP(String host) throws IOException
046    {
047        int lines = 100;
048        String line;
049        CharGenTCPClient client = new CharGenTCPClient();
050        BufferedReader chargenInput;
051
052        // We want to timeout if a response takes longer than 60 seconds
053        client.setDefaultTimeout(60000);
054        client.connect(host);
055        chargenInput =
056            new BufferedReader(new InputStreamReader(client.getInputStream()));
057
058        // We assume the chargen service outputs lines, but it really doesn't
059        // have to, so this code might actually not work if no newlines are
060        // present.
061        while (lines-- > 0)
062        {
063            if ((line = chargenInput.readLine()) == null) {
064                break;
065            }
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 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