001    /*
002     * Copyright 2001-2005 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.commons.net;
017    
018    import java.io.IOException;
019    import java.net.DatagramPacket;
020    import java.net.InetAddress;
021    
022    /***
023     * The DiscardUDPClient class is a UDP implementation of a client for the
024     * Discard protocol described in RFC 863.  To use the class,
025     * just open a local UDP port
026     * with {@link org.apache.commons.net.DatagramSocketClient#open  open }
027     * and call {@link #send  send } to send datagrams to the server
028     * After you're done sending discard data, call
029     * {@link org.apache.commons.net.DatagramSocketClient#close  close() }
030     * to clean up properly.
031     * <p>
032     * <p>
033     * @author Daniel F. Savarese
034     * @see DiscardTCPClient
035     ***/
036    
037    public class DiscardUDPClient extends DatagramSocketClient
038    {
039        /*** The default discard port.  It is set to 9 according to RFC 863. ***/
040        public static final int DEFAULT_PORT = 9;
041    
042        DatagramPacket _sendPacket;
043    
044        public DiscardUDPClient()
045        {
046            _sendPacket = new DatagramPacket(new byte[0], 0);
047        }
048    
049    
050        /***
051         * Sends the specified data to the specified server at the specified port.
052         * <p>
053         * @param data  The discard data to send.
054         * @param length  The length of the data to send.  Should be less than
055         *    or equal to the length of the data byte array.
056         * @param host  The address of the server.
057         * @param port  The service port.
058         * @exception IOException If an error occurs during the datagram send
059         *            operation.
060         ***/
061        public void send(byte[] data, int length, InetAddress host, int port)
062        throws IOException
063        {
064            _sendPacket.setData(data);
065            _sendPacket.setLength(length);
066            _sendPacket.setAddress(host);
067            _sendPacket.setPort(port);
068            _socket_.send(_sendPacket);
069        }
070    
071    
072        /***
073         * Same as
074         * <code>send(data, length, host. DiscardUDPClient.DEFAULT_PORT)</code>.
075         ***/
076        public void send(byte[] data, int length, InetAddress host)
077        throws IOException
078        {
079            send(data, length, host, DEFAULT_PORT);
080        }
081    
082    
083        /***
084         * Same as
085         * <code>send(data, data.length, host. DiscardUDPClient.DEFAULT_PORT)</code>.
086         ***/
087        public void send(byte[] data, InetAddress host) throws IOException
088        {
089            send(data, data.length, host, DEFAULT_PORT);
090        }
091    
092    }
093