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