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