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     * @see DiscardTCPClient
038     ***/
039    
040    public class DiscardUDPClient extends DatagramSocketClient
041    {
042        /*** The default discard port.  It is set to 9 according to RFC 863. ***/
043        public static final int DEFAULT_PORT = 9;
044    
045        DatagramPacket _sendPacket;
046    
047        public DiscardUDPClient()
048        {
049            _sendPacket = new DatagramPacket(new byte[0], 0);
050        }
051    
052    
053        /***
054         * Sends the specified data to the specified server at the specified port.
055         * <p>
056         * @param data  The discard data to send.
057         * @param length  The length of the data to send.  Should be less than
058         *    or equal to the length of the data byte array.
059         * @param host  The address of the server.
060         * @param port  The service port.
061         * @exception IOException If an error occurs during the datagram send
062         *            operation.
063         ***/
064        public void send(byte[] data, int length, InetAddress host, int port)
065        throws IOException
066        {
067            _sendPacket.setData(data);
068            _sendPacket.setLength(length);
069            _sendPacket.setAddress(host);
070            _sendPacket.setPort(port);
071            _socket_.send(_sendPacket);
072        }
073    
074    
075        /***
076         * Same as
077         * <code>send(data, length, host. DiscardUDPClient.DEFAULT_PORT)</code>.
078         ***/
079        public void send(byte[] data, int length, InetAddress host)
080        throws IOException
081        {
082            send(data, length, host, DEFAULT_PORT);
083        }
084    
085    
086        /***
087         * Same as
088         * <code>send(data, data.length, host. DiscardUDPClient.DEFAULT_PORT)</code>.
089         ***/
090        public void send(byte[] data, InetAddress host) throws IOException
091        {
092            send(data, data.length, host, DEFAULT_PORT);
093        }
094    
095    }
096