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