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
038    /** The default echo port. It is set to 7 according to RFC 862. */
039    public static final int DEFAULT_PORT = 7;
040
041    private final DatagramPacket receivePacket = new DatagramPacket(NetConstants.EMPTY_BTYE_ARRAY, 0);
042
043    /**
044     * Constructs a new instance.
045     */
046    public EchoUDPClient() {
047        // empty
048    }
049
050    /**
051     * Same as {@code receive(data, data.length)}
052     *
053     * @param data the buffer to receive the input
054     * @return the number of bytes
055     * @throws IOException on error
056     */
057    public int receive(final byte[] data) throws IOException {
058        return receive(data, data.length);
059    }
060
061    /**
062     * Receives echoed data and returns its length. The data may be divided up among multiple datagrams, requiring multiple calls to receive. Also, the UDP
063     * packets will not necessarily arrive in the same order they were sent.
064     *
065     * @param data   the buffer to receive the input
066     * @param length of the buffer
067     * @return Length of actual data received.
068     * @throws IOException If an error occurs while receiving the data.
069     */
070    public int receive(final byte[] data, final int length) throws IOException {
071        receivePacket.setData(data);
072        receivePacket.setLength(length);
073        checkOpen().receive(receivePacket);
074        return receivePacket.getLength();
075    }
076
077    /** Same as {@code send(data, data.length, host)} */
078    @Override
079    public void send(final byte[] data, final InetAddress host) throws IOException {
080        send(data, data.length, host, DEFAULT_PORT);
081    }
082
083    /**
084     * Sends the specified data to the specified server at the default echo port.
085     *
086     * @param data   The echo data to send.
087     * @param length The length of the data to send. Should be less than or equal to the length of the data byte array.
088     * @param host   The address of the server.
089     * @throws IOException If an error occurs during the datagram send operation.
090     */
091    @Override
092    public void send(final byte[] data, final int length, final InetAddress host) throws IOException {
093        send(data, length, host, DEFAULT_PORT);
094    }
095
096}