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