    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;
    025import org.apache.commons.net.util.NetConstants;
    026
    027/**
    028 * The DiscardUDPClient class is a UDP implementation of a client for the
    029 * Discard protocol described in RFC 863.  To use the class,
    030 * just open a local UDP port
    031 * with {@link org.apache.commons.net.DatagramSocketClient#open  open }
    032 * and call {@link #send  send } to send datagrams to the server
    033 * After you're done sending discard data, call
    034 * {@link org.apache.commons.net.DatagramSocketClient#close  close() }
    035 * to clean up properly.
    036 *
    037 * @see DiscardTCPClient
    038 */
    039
    040public 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    private final DatagramPacket sendPacket;
    046
    047    public DiscardUDPClient()
    048    {
    049        sendPacket = new DatagramPacket(NetConstants.EMPTY_BTYE_ARRAY, 0);
    050    }
    051
    052
    053    /**
    054     * Sends the specified data to the specified server at the specified port.
    055     *
    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     * @throws IOException If an error occurs during the datagram send
    062     *            operation.
    063     */
    064    public void send(final byte[] data, final int length, final InetAddress host, final 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     * @param data the buffer to send
    079     * @param length the length of the data in the buffer
    080     * @param host the target host
    081     * @see #send(byte[], int, InetAddress, int)
    082     * @throws IOException if an error occurs
    083     */
    084    public void send(final byte[] data, final int length, final InetAddress host)
    085    throws IOException
    086    {
    087        send(data, length, host, DEFAULT_PORT);
    088    }
    089
    090
    091    /**
    092     * Same as
    093     * <code>send(data, data.length, host. DiscardUDPClient.DEFAULT_PORT)</code>.
    094     * @param data the buffer to send
    095     * @param host the target host
    096     * @see #send(byte[], int, InetAddress, int)
    097     * @throws IOException if an error occurs
    098     */
    099    public void send(final byte[] data, final InetAddress host) throws IOException
    100    {
    101        send(data, data.length, host, DEFAULT_PORT);
    102    }
    103
    104}
    105