    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.discard;
    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 DiscardUDPClient class is a UDP implementation of a client for the
    028     * Discard protocol described in RFC 863.  To use the class,
    029     * just open a local UDP port
    030     * with {@link org.apache.commons.net.DatagramSocketClient#open  open }
    031     * and call {@link #send  send } to send datagrams to the server
    032     * After you're done sending discard data, call
    033     * {@link org.apache.commons.net.DatagramSocketClient#close  close() }
    034     * to clean up properly.
    035     * <p>
    036     * <p>
    037     * @author Daniel F. Savarese
    038     * @see DiscardTCPClient
    039     ***/
    040
    041    public class DiscardUDPClient extends DatagramSocketClient
    042    {
    043        /*** The default discard port.  It is set to 9 according to RFC 863. ***/
    044        public static final int DEFAULT_PORT = 9;
    045
    046        DatagramPacket _sendPacket;
    047
    048        public DiscardUDPClient()
    049        {
    050            _sendPacket = new DatagramPacket(new byte[0], 0);
    051        }
    052
    053
    054        /***
    055         * Sends the specified data to the specified server at the specified port.
    056         * <p>
    057         * @param data  The discard data to send.
    058         * @param length  The length of the data to send.  Should be less than
    059         *    or equal to the length of the data byte array.
    060         * @param host  The address of the server.
    061         * @param port  The service port.
    062         * @exception IOException If an error occurs during the datagram send
    063         *            operation.
    064         ***/
    065        public void send(byte[] data, int length, InetAddress host, int port)
    066        throws IOException
    067        {
    068            _sendPacket.setData(data);
    069            _sendPacket.setLength(length);
    070            _sendPacket.setAddress(host);
    071            _sendPacket.setPort(port);
    072            _socket_.send(_sendPacket);
    073        }
    074
    075
    076        /***
    077         * Same as
    078         * <code>send(data, length, host. DiscardUDPClient.DEFAULT_PORT)</code>.
    079         ***/
    080        public void send(byte[] data, int length, InetAddress host)
    081        throws IOException
    082        {
    083            send(data, length, host, DEFAULT_PORT);
    084        }
    085
    086
    087        /***
    088         * Same as
    089         * <code>send(data, data.length, host. DiscardUDPClient.DEFAULT_PORT)</code>.
    090         ***/
    091        public void send(byte[] data, InetAddress host) throws IOException
    092        {
    093            send(data, data.length, host, DEFAULT_PORT);
    094        }
    095
    096    }
    097