    View Javadoc
    1   package org.apache.commons.net;
    2
    3   /* ====================================================================
    4    * The Apache Software License, Version 1.1
    5    *
    6    * Copyright (c) 2001 The Apache Software Foundation.  All rights
    7    * reserved.
    8    *
    9    * Redistribution and use in source and binary forms, with or without
    10   * modification, are permitted provided that the following conditions
    11   * are met:
    12   *
    13   * 1. Redistributions of source code must retain the above copyright
    14   *    notice, this list of conditions and the following disclaimer.
    15   *
    16   * 2. Redistributions in binary form must reproduce the above copyright
    17   *    notice, this list of conditions and the following disclaimer in
    18   *    the documentation and/or other materials provided with the
    19   *    distribution.
    20   *
    21   * 3. The end-user documentation included with the redistribution,
    22   *    if any, must include the following acknowledgment:
    23   *       "This product includes software developed by the
    24   *        Apache Software Foundation (http://www.apache.org/)."
    25   *    Alternately, this acknowledgment may appear in the software itself,
    26   *    if and wherever such third-party acknowledgments normally appear.
    27   *
    28   * 4. The names "Apache" and "Apache Software Foundation" and
    29   *    "Apache Commons" must not be used to endorse or promote products
    30   *    derived from this software without prior written permission. For
    31   *    written permission, please contact apache@apache.org.
    32   *
    33   * 5. Products derived from this software may not be called "Apache",
    34   *    nor may "Apache" appear in their name, without
    35   *    prior written permission of the Apache Software Foundation.
    36   *
    37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
    38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
    41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
    44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    48   * SUCH DAMAGE.
    49   * ====================================================================
    50   *
    51   * This software consists of voluntary contributions made by many
    52   * individuals on behalf of the Apache Software Foundation.  For more
    53   * information on the Apache Software Foundation, please see
    54   * <http://www.apache.org/>;.
    55   */
    56
    57  import java.io.IOException;
    58  import java.net.DatagramPacket;
    59  import java.net.InetAddress;
    60
    61  /****
    62   * The CharGenUDPClient class is a UDP implementation of a client for the
    63   * character generator protocol described in RFC 864.  It can also be
    64   * used for Systat (RFC 866), Quote of the Day (RFC 865), and netstat
    65   * (port 15).  All of these protocols involve sending a datagram to the
    66   * appropriate port, and reading data contained in one or more reply
    67   * datagrams.  The chargen and quote of the day protocols only send
    68   * one reply datagram containing 512 bytes or less of data.  The other
    69   * protocols may reply with more than one datagram, in which case you
    70   * must wait for a timeout to determine that all reply datagrams have
    71   * been sent.
    72   * <p>
    73   * To use the CharGenUDPClient class, just open a local UDP port
    74   * with <a href="org.apache.commons.net.DatagramSocketClient.html#open"> open </a>
    75   * and call <a href="#send"> send </a> to send the datagram that will
    76   * initiate the data reply.  For chargen or quote of the day, just
    77   * call <a href="#recieve"> receive </a>, and you're done.  For netstat and
    78   * systat, call receive in a while loop, and catch a SocketException and
    79   * InterruptedIOException to detect a timeout (don't forget to set the
    80   * timeout duration beforehand).  Don't forget to call
    81   * <a href="org.apache.commons.net.DatagramSocketClient.html#close"> close() </a>
    82   * to clean up properly.
    83   * <p>
    84   * <p>
    85   * @author Daniel F. Savarese
    86   * @see CharGenTCPClient
    87   ***/
    88
    89  public final class CharGenUDPClient extends DatagramSocketClient
    90  {
    91      /**** The systat port value of 11 according to RFC 866. ***/
    92      public static final int SYSTAT_PORT = 11;
    93      /**** The netstat port value of 19. ***/
    94      public static final int NETSTAT_PORT = 15;
    95      /**** The quote of the day port value of 17 according to RFC 865. ***/
    96      public static final int QUOTE_OF_DAY_PORT = 17;
    97      /**** The character generator port value of 19 according to RFC 864. ***/
    98      public static final int CHARGEN_PORT = 19;
    99      /**** The default chargen port.  It is set to 19 according to RFC 864. ***/
    100     public static final int DEFAULT_PORT = 19;
    101
    102     private byte[] __receiveData;
    103     private DatagramPacket __receivePacket;
    104     private DatagramPacket __sendPacket;
    105
    106     /****
    107      * The default CharGenUDPClient constructor.  It initializes some internal
    108      * data structures for sending and receiving the necessary datagrams for
    109      * the chargen and related protocols.
    110      ***/
    111     public CharGenUDPClient()
    112     {
    113         // CharGen return packets have a maximum length of 512
    114         __receiveData = new byte[512];
    115         __receivePacket = new DatagramPacket(__receiveData, 512);
    116         __sendPacket = new DatagramPacket(new byte[0], 0);
    117     }
    118
    119
    120     /****
    121      * Sends the data initiation datagram.  This data in the packet is ignored
    122      * by the server, and merely serves to signal that the server should send
    123      * its reply.
    124      * <p>
    125      * @param host The address of the server.
    126      * @param port The port of the service.
    127      * @exception IOException If an error occurs while sending the datagram.
    128      ***/
    129     public void send(InetAddress host, int port) throws IOException
    130     {
    131         __sendPacket.setAddress(host);
    132         __sendPacket.setPort(port);
    133         _socket_.send(__sendPacket);
    134     }
    135
    136     /**** Same as <code>send(host, CharGenUDPClient.DEFAULT_PORT);</code> ***/
    137     public void send(InetAddress host) throws IOException
    138     {
    139         send(host, DEFAULT_PORT);
    140     }
    141
    142     /****
    143      * Receive the reply data from the server.  This will always be 512 bytes
    144      * or less.  Chargen and quote of the day only return one packet.  Netstat
    145      * and systat require multiple calls to receive() with timeout detection.
    146      * <p>
    147      * @return The reply data from the server.
    148      * @exception IOException If an error occurs while receiving the datagram.
    149      ***/
    150     public byte[] receive() throws IOException
    151     {
    152         int length;
    153         byte[] result;
    154
    155         _socket_.receive(__receivePacket);
    156
    157         result = new byte[length = __receivePacket.getLength()];
    158         System.arraycopy(__receiveData, 0, result, 0, length);
    159
    160         return result;
    161     }
    162
    163 }
    164

This page was automatically generated by Maven