DiscardUDPClient.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net.discard;

  18. import java.io.IOException;
  19. import java.net.DatagramPacket;
  20. import java.net.InetAddress;

  21. import org.apache.commons.net.DatagramSocketClient;
  22. import org.apache.commons.net.util.NetConstants;

  23. /**
  24.  * 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
  25.  * 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
  26.  * discard data, call {@link org.apache.commons.net.DatagramSocketClient#close close() } to clean up properly.
  27.  *
  28.  * @see DiscardTCPClient
  29.  */
  30. public class DiscardUDPClient extends DatagramSocketClient {

  31.     /** The default discard port. It is set to 9 according to RFC 863. */
  32.     public static final int DEFAULT_PORT = 9;

  33.     private final DatagramPacket sendPacket;

  34.     public DiscardUDPClient() {
  35.         sendPacket = new DatagramPacket(NetConstants.EMPTY_BTYE_ARRAY, 0);
  36.     }

  37.     /**
  38.      * Same as <code>send(data, data.length, host. DiscardUDPClient.DEFAULT_PORT)</code>.
  39.      *
  40.      * @param data the buffer to send
  41.      * @param host the target host
  42.      * @see #send(byte[], int, InetAddress, int)
  43.      * @throws IOException if an error occurs
  44.      */
  45.     public void send(final byte[] data, final InetAddress host) throws IOException {
  46.         send(data, data.length, host, DEFAULT_PORT);
  47.     }

  48.     /**
  49.      * Same as <code>send(data, length, host. DiscardUDPClient.DEFAULT_PORT)</code>.
  50.      *
  51.      * @param data   the buffer to send
  52.      * @param length the length of the data in the buffer
  53.      * @param host   the target host
  54.      * @see #send(byte[], int, InetAddress, int)
  55.      * @throws IOException if an error occurs
  56.      */
  57.     public void send(final byte[] data, final int length, final InetAddress host) throws IOException {
  58.         send(data, length, host, DEFAULT_PORT);
  59.     }

  60.     /**
  61.      * Sends the specified data to the specified server at the specified port.
  62.      *
  63.      * @param data   The discard data to send.
  64.      * @param length The length of the data to send. Should be less than or equal to the length of the data byte array.
  65.      * @param host   The address of the server.
  66.      * @param port   The service port.
  67.      * @throws IOException If an error occurs during the datagram send operation.
  68.      */
  69.     public void send(final byte[] data, final int length, final InetAddress host, final int port) throws IOException {
  70.         sendPacket.setData(data);
  71.         sendPacket.setLength(length);
  72.         sendPacket.setAddress(host);
  73.         sendPacket.setPort(port);
  74.         checkOpen().send(sendPacket);
  75.     }

  76. }