View Javadoc

    1   /*
    2    * Copyright 2001-2005 The Apache Software Foundation
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.net;
    17
    18  import java.io.IOException;
    19  import java.net.DatagramPacket;
    20  import java.net.InetAddress;
    21
    22  /****
    23   * The CharGenUDPClient class is a UDP implementation of a client for the
    24   * character generator protocol described in RFC 864.  It can also be
    25   * used for Systat (RFC 866), Quote of the Day (RFC 865), and netstat
    26   * (port 15).  All of these protocols involve sending a datagram to the
    27   * appropriate port, and reading data contained in one or more reply
    28   * datagrams.  The chargen and quote of the day protocols only send
    29   * one reply datagram containing 512 bytes or less of data.  The other
    30   * protocols may reply with more than one datagram, in which case you
    31   * must wait for a timeout to determine that all reply datagrams have
    32   * been sent.
    33   * <p>
    34   * To use the CharGenUDPClient class, just open a local UDP port
    35   * with {@link org.apache.commons.net.DatagramSocketClient#open  open }
    36   * and call {@link #send  send } to send the datagram that will
    37   * initiate the data reply.  For chargen or quote of the day, just
    38   * call {@link #receive  receive }, and you're done.  For netstat and
    39   * systat, call receive in a while loop, and catch a SocketException and
    40   * InterruptedIOException to detect a timeout (don't forget to set the
    41   * timeout duration beforehand).  Don't forget to call
    42   * {@link org.apache.commons.net.DatagramSocketClient#close  close() }
    43   * to clean up properly.
    44   * <p>
    45   * <p>
    46   * @author Daniel F. Savarese
    47   * @see CharGenTCPClient
    48   ***/
    49
    50  public final class CharGenUDPClient extends DatagramSocketClient
    51  {
    52      /**** The systat port value of 11 according to RFC 866. ***/
    53      public static final int SYSTAT_PORT = 11;
    54      /**** The netstat port value of 19. ***/
    55      public static final int NETSTAT_PORT = 15;
    56      /**** The quote of the day port value of 17 according to RFC 865. ***/
    57      public static final int QUOTE_OF_DAY_PORT = 17;
    58      /**** The character generator port value of 19 according to RFC 864. ***/
    59      public static final int CHARGEN_PORT = 19;
    60      /**** The default chargen port.  It is set to 19 according to RFC 864. ***/
    61      public static final int DEFAULT_PORT = 19;
    62
    63      private byte[] __receiveData;
    64      private DatagramPacket __receivePacket;
    65      private DatagramPacket __sendPacket;
    66
    67      /****
    68       * The default CharGenUDPClient constructor.  It initializes some internal
    69       * data structures for sending and receiving the necessary datagrams for
    70       * the chargen and related protocols.
    71       ***/
    72      public CharGenUDPClient()
    73      {
    74          // CharGen return packets have a maximum length of 512
    75          __receiveData = new byte[512];
    76          __receivePacket = new DatagramPacket(__receiveData, 512);
    77          __sendPacket = new DatagramPacket(new byte[0], 0);
    78      }
    79
    80
    81      /****
    82       * Sends the data initiation datagram.  This data in the packet is ignored
    83       * by the server, and merely serves to signal that the server should send
    84       * its reply.
    85       * <p>
    86       * @param host The address of the server.
    87       * @param port The port of the service.
    88       * @exception IOException If an error occurs while sending the datagram.
    89       ***/
    90      public void send(InetAddress host, int port) throws IOException
    91      {
    92          __sendPacket.setAddress(host);
    93          __sendPacket.setPort(port);
    94          _socket_.send(__sendPacket);
    95      }
    96
    97      /**** Same as <code>send(host, CharGenUDPClient.DEFAULT_PORT);</code> ***/
    98      public void send(InetAddress host) throws IOException
    99      {
    100         send(host, DEFAULT_PORT);
    101     }
    102
    103     /****
    104      * Receive the reply data from the server.  This will always be 512 bytes
    105      * or less.  Chargen and quote of the day only return one packet.  Netstat
    106      * and systat require multiple calls to receive() with timeout detection.
    107      * <p>
    108      * @return The reply data from the server.
    109      * @exception IOException If an error occurs while receiving the datagram.
    110      ***/
    111     public byte[] receive() throws IOException
    112     {
    113         int length;
    114         byte[] result;
    115
    116         _socket_.receive(__receivePacket);
    117
    118         result = new byte[length = __receivePacket.getLength()];
    119         System.arraycopy(__receiveData, 0, result, 0, length);
    120
    121         return result;
    122     }
    123
    124 }
    125