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.echo;
019
020import java.io.IOException;
021import java.net.DatagramPacket;
022import java.net.InetAddress;
023
024import org.apache.commons.net.discard.DiscardUDPClient;
025import org.apache.commons.net.util.NetConstants;
026
027/**
028 * 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
029 * {@link org.apache.commons.net.DatagramSocketClient#open open} and call {@link #send send} to send datagrams to the server, then call {@link #receive
030 * receive } to receive echoes. After you're done echoing data, call {@link org.apache.commons.net.DatagramSocketClient#close close()} to clean up properly.
031 *
032 * @see EchoTCPClient
033 * @see DiscardUDPClient
034 */
035
036public final class EchoUDPClient extends DiscardUDPClient {
037    /** The default echo port. It is set to 7 according to RFC 862. */
038    public static final int DEFAULT_PORT = 7;
039
040    private final DatagramPacket receivePacket = new DatagramPacket(NetConstants.EMPTY_BTYE_ARRAY, 0);
041
042    /**
043     * Constructs a new instance.
044     */
045    public EchoUDPClient() {
046        // empty
047    }
048
049    /**
050     * Same as {@code receive(data, data.length)}
051     *
052     * @param data the buffer to receive the input
053     * @return the number of bytes
054     * @throws IOException on error
055     */
056    public int receive(final byte[] data) throws IOException {
057        return receive(data, data.length);
058    }
059
060    /**
061     * Receives echoed data and returns its length. The data may be divided up among multiple datagrams, requiring multiple calls to receive. Also, the UDP
062     * packets will not necessarily arrive in the same order they were sent.
063     *
064     * @param data   the buffer to receive the input
065     * @param length of the buffer
066     * @return Length of actual data received.
067     * @throws IOException If an error occurs while receiving the data.
068     */
069    public int receive(final byte[] data, final int length) throws IOException {
070        receivePacket.setData(data);
071        receivePacket.setLength(length);
072        checkOpen().receive(receivePacket);
073        return receivePacket.getLength();
074    }
075
076    /** Same as {@code send(data, data.length, host)} */
077    @Override
078    public void send(final byte[] data, final InetAddress host) throws IOException {
079        send(data, data.length, host, DEFAULT_PORT);
080    }
081
082    /**
083     * Sends the specified data to the specified server at the default echo port.
084     *
085     * @param data   The echo data to send.
086     * @param length The length of the data to send. Should be less than or equal to the length of the data byte array.
087     * @param host   The address of the server.
088     * @throws IOException If an error occurs during the datagram send operation.
089     */
090    @Override
091    public void send(final byte[] data, final int length, final InetAddress host) throws IOException {
092        send(data, length, host, DEFAULT_PORT);
093    }
094
095}