EchoUDPClient.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.  *      https://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.echo;

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

  21. import org.apache.commons.net.discard.DiscardUDPClient;
  22. import org.apache.commons.net.util.NetConstants;

  23. /**
  24.  * The EchoUDPClient class is a UDP implementation of a client for the Echo protocol described in RFC 862. To use the class, just open a local UDP port with
  25.  * {@link org.apache.commons.net.DatagramSocketClient#open open} and call {@link #send send} to send datagrams to the server, then call {@link #receive
  26.  * receive } to receive echoes. After you're done echoing data, call {@link org.apache.commons.net.DatagramSocketClient#close close()} to clean up properly.
  27.  *
  28.  * @see EchoTCPClient
  29.  * @see DiscardUDPClient
  30.  */

  31. public final class EchoUDPClient extends DiscardUDPClient {
  32.     /** The default echo port. It is set to 7 according to RFC 862. */
  33.     public static final int DEFAULT_PORT = 7;

  34.     private final DatagramPacket receivePacket = new DatagramPacket(NetConstants.EMPTY_BTYE_ARRAY, 0);

  35.     /**
  36.      * Constructs a new instance.
  37.      */
  38.     public EchoUDPClient() {
  39.         // empty
  40.     }

  41.     /**
  42.      * Same as {@code receive(data, data.length)}
  43.      *
  44.      * @param data the buffer to receive the input
  45.      * @return the number of bytes
  46.      * @throws IOException on error
  47.      */
  48.     public int receive(final byte[] data) throws IOException {
  49.         return receive(data, data.length);
  50.     }

  51.     /**
  52.      * Receives echoed data and returns its length. The data may be divided up among multiple datagrams, requiring multiple calls to receive. Also, the UDP
  53.      * packets will not necessarily arrive in the same order they were sent.
  54.      *
  55.      * @param data   the buffer to receive the input
  56.      * @param length of the buffer
  57.      * @return Length of actual data received.
  58.      * @throws IOException If an error occurs while receiving the data.
  59.      */
  60.     public int receive(final byte[] data, final int length) throws IOException {
  61.         receivePacket.setData(data);
  62.         receivePacket.setLength(length);
  63.         checkOpen().receive(receivePacket);
  64.         return receivePacket.getLength();
  65.     }

  66.     /** Same as {@code send(data, data.length, host)} */
  67.     @Override
  68.     public void send(final byte[] data, final InetAddress host) throws IOException {
  69.         send(data, data.length, host, DEFAULT_PORT);
  70.     }

  71.     /**
  72.      * Sends the specified data to the specified server at the default echo port.
  73.      *
  74.      * @param data   The echo data to send.
  75.      * @param length The length of the data to send. Should be less than or equal to the length of the data byte array.
  76.      * @param host   The address of the server.
  77.      * @throws IOException If an error occurs during the datagram send operation.
  78.      */
  79.     @Override
  80.     public void send(final byte[] data, final int length, final InetAddress host) throws IOException {
  81.         send(data, length, host, DEFAULT_PORT);
  82.     }

  83. }