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.chargen;
019
020import java.io.IOException;
021import java.net.DatagramPacket;
022import java.net.InetAddress;
023
024import 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 * @author Daniel F. Savarese
051 * @see CharGenTCPClient
052 ***/
053
054public final class CharGenUDPClient extends DatagramSocketClient
055{
056    /*** The systat port value of 11 according to RFC 866. ***/
057    public static final int SYSTAT_PORT = 11;
058    /*** The netstat port value of 19. ***/
059    public static final int NETSTAT_PORT = 15;
060    /*** The quote of the day port value of 17 according to RFC 865. ***/
061    public static final int QUOTE_OF_DAY_PORT = 17;
062    /*** The character generator port value of 19 according to RFC 864. ***/
063    public static final int CHARGEN_PORT = 19;
064    /*** The default chargen port.  It is set to 19 according to RFC 864. ***/
065    public static final int DEFAULT_PORT = 19;
066
067    private byte[] __receiveData;
068    private DatagramPacket __receivePacket;
069    private DatagramPacket __sendPacket;
070
071    /***
072     * The default CharGenUDPClient constructor.  It initializes some internal
073     * data structures for sending and receiving the necessary datagrams for
074     * the chargen and related protocols.
075     ***/
076    public CharGenUDPClient()
077    {
078        // CharGen return packets have a maximum length of 512
079        __receiveData = new byte[512];
080        __receivePacket = new DatagramPacket(__receiveData, 512);
081        __sendPacket = new DatagramPacket(new byte[0], 0);
082    }
083
084
085    /***
086     * Sends the data initiation datagram.  This data in the packet is ignored
087     * by the server, and merely serves to signal that the server should send
088     * its reply.
089     * <p>
090     * @param host The address of the server.
091     * @param port The port of the service.
092     * @exception IOException If an error occurs while sending the datagram.
093     ***/
094    public void send(InetAddress host, int port) throws IOException
095    {
096        __sendPacket.setAddress(host);
097        __sendPacket.setPort(port);
098        _socket_.send(__sendPacket);
099    }
100
101    /*** Same as <code>send(host, CharGenUDPClient.DEFAULT_PORT);</code> ***/
102    public void send(InetAddress host) throws IOException
103    {
104        send(host, DEFAULT_PORT);
105    }
106
107    /***
108     * Receive the reply data from the server.  This will always be 512 bytes
109     * or less.  Chargen and quote of the day only return one packet.  Netstat
110     * and systat require multiple calls to receive() with timeout detection.
111     * <p>
112     * @return The reply data from the server.
113     * @exception IOException If an error occurs while receiving the datagram.
114     ***/
115    public byte[] receive() throws IOException
116    {
117        int length;
118        byte[] result;
119
120        _socket_.receive(__receivePacket);
121
122        result = new byte[length = __receivePacket.getLength()];
123        System.arraycopy(__receiveData, 0, result, 0, length);
124
125        return result;
126    }
127
128}
129