    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 org.apache.commons.net.chargen;
    019
    020    import java.io.IOException;
    021    import java.net.DatagramPacket;
    022    import java.net.InetAddress;
    023
    024    import org.apache.commons.net.DatagramSocketClient;
    025
    026    /***
    027     * The CharGenUDPClient class is a UDP implementation of a client for the
    028     * character generator protocol described in RFC 864.  It can also be
    029     * used for Systat (RFC 866), Quote of the Day (RFC 865), and netstat
    030     * (port 15).  All of these protocols involve sending a datagram to the
    031     * appropriate port, and reading data contained in one or more reply
    032     * datagrams.  The chargen and quote of the day protocols only send
    033     * one reply datagram containing 512 bytes or less of data.  The other
    034     * protocols may reply with more than one datagram, in which case you
    035     * must wait for a timeout to determine that all reply datagrams have
    036     * been sent.
    037     * <p>
    038     * To use the CharGenUDPClient class, just open a local UDP port
    039     * with {@link org.apache.commons.net.DatagramSocketClient#open  open }
    040     * and call {@link #send  send } to send the datagram that will
    041     * initiate the data reply.  For chargen or quote of the day, just
    042     * call {@link #receive  receive }, and you're done.  For netstat and
    043     * systat, call receive in a while loop, and catch a SocketException and
    044     * InterruptedIOException to detect a timeout (don't forget to set the
    045     * timeout duration beforehand).  Don't forget to call
    046     * {@link org.apache.commons.net.DatagramSocketClient#close  close() }
    047     * to clean up properly.
    048     * <p>
    049     * <p>
    050     * @see CharGenTCPClient
    051     ***/
    052
    053    public final class CharGenUDPClient extends DatagramSocketClient
    054    {
    055        /*** The systat port value of 11 according to RFC 866. ***/
    056        public static final int SYSTAT_PORT = 11;
    057        /*** The netstat port value of 19. ***/
    058        public static final int NETSTAT_PORT = 15;
    059        /*** The quote of the day port value of 17 according to RFC 865. ***/
    060        public static final int QUOTE_OF_DAY_PORT = 17;
    061        /*** The character generator port value of 19 according to RFC 864. ***/
    062        public static final int CHARGEN_PORT = 19;
    063        /*** The default chargen port.  It is set to 19 according to RFC 864. ***/
    064        public static final int DEFAULT_PORT = 19;
    065
    066        private final byte[] __receiveData;
    067        private final DatagramPacket __receivePacket;
    068        private final DatagramPacket __sendPacket;
    069
    070        /***
    071         * The default CharGenUDPClient constructor.  It initializes some internal
    072         * data structures for sending and receiving the necessary datagrams for
    073         * the chargen and related protocols.
    074         ***/
    075        public CharGenUDPClient()
    076        {
    077            // CharGen return packets have a maximum length of 512
    078            __receiveData = new byte[512];
    079            __receivePacket = new DatagramPacket(__receiveData, __receiveData.length);
    080            __sendPacket = new DatagramPacket(new byte[0], 0);
    081        }
    082
    083
    084        /***
    085         * Sends the data initiation datagram.  This data in the packet is ignored
    086         * by the server, and merely serves to signal that the server should send
    087         * its reply.
    088         * <p>
    089         * @param host The address of the server.
    090         * @param port The port of the service.
    091         * @exception IOException If an error occurs while sending the datagram.
    092         ***/
    093        public void send(InetAddress host, int port) throws IOException
    094        {
    095            __sendPacket.setAddress(host);
    096            __sendPacket.setPort(port);
    097            _socket_.send(__sendPacket);
    098        }
    099
    100        /*** Same as <code>send(host, CharGenUDPClient.DEFAULT_PORT);</code> ***/
    101        public void send(InetAddress host) throws IOException
    102        {
    103            send(host, DEFAULT_PORT);
    104        }
    105
    106        /***
    107         * Receive the reply data from the server.  This will always be 512 bytes
    108         * or less.  Chargen and quote of the day only return one packet.  Netstat
    109         * and systat require multiple calls to receive() with timeout detection.
    110         * <p>
    111         * @return The reply data from the server.
    112         * @exception IOException If an error occurs while receiving the datagram.
    113         ***/
    114        public byte[] receive() throws IOException
    115        {
    116            int length;
    117            byte[] result;
    118
    119            _socket_.receive(__receivePacket);
    120
    121            result = new byte[length = __receivePacket.getLength()];
    122            System.arraycopy(__receiveData, 0, result, 0, length);
    123
    124            return result;
    125        }
    126
    127    }
    128