001    /*
002     * Copyright 2001-2005 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.commons.net;
017    
018    import java.io.IOException;
019    import java.net.DatagramPacket;
020    import java.net.InetAddress;
021    
022    /***
023     * The EchoUDPClient class is a UDP implementation of a client for the
024     * Echo protocol described in RFC 862.  To use the class,
025     * just open a local UDP port
026     * with {@link org.apache.commons.net.DatagramSocketClient#open  open }
027     * and call {@link #send  send } to send datagrams to the server,
028     * then call {@link #receive  receive } to receive echoes.
029     * After you're done echoing data, call
030     * {@link org.apache.commons.net.DatagramSocketClient#close  close() }
031     * to clean up properly.
032     * <p>
033     * <p>
034     * @author Daniel F. Savarese
035     * @see EchoTCPClient
036     * @see DiscardUDPClient
037     ***/
038    
039    public final class EchoUDPClient extends DiscardUDPClient
040    {
041        /*** The default echo port.  It is set to 7 according to RFC 862. ***/
042        public static final int DEFAULT_PORT = 7;
043    
044        private DatagramPacket __receivePacket = new DatagramPacket(new byte[0], 0);
045    
046        /***
047         * Sends the specified data to the specified server at the default echo
048         * port.
049         * <p>
050         * @param data  The echo data to send.
051         * @param length  The length of the data to send.  Should be less than
052         *    or equal to the length of the data byte array.
053         * @param host  The address of the server.
054         * @exception IOException If an error occurs during the datagram send
055         *     operation.
056         ***/
057        public void send(byte[] data, int length, InetAddress host)
058        throws IOException
059        {
060            send(data, length, host, DEFAULT_PORT);
061        }
062    
063    
064        /*** Same as <code> send(data, data.length, host) </code> ***/
065        public void send(byte[] data, InetAddress host) throws IOException
066        {
067            send(data, data.length, host, DEFAULT_PORT);
068        }
069    
070    
071        /***
072         * Receives echoed data and returns its length.  The data may be divided
073         * up among multiple datagrams, requiring multiple calls to receive.
074         * Also, the UDP packets will not necessarily arrive in the same order
075         * they were sent.
076         * <p>
077         * @return  Length of actual data received.
078         * @exception IOException If an error occurs while receiving the data.
079         ***/
080        public int receive(byte[] data, int length) throws IOException
081        {
082            __receivePacket.setData(data);
083            __receivePacket.setLength(length);
084            _socket_.receive(__receivePacket);
085            return __receivePacket.getLength();
086        }
087    
088        /*** Same as <code> receive(data, data.length)</code> ***/
089        public int receive(byte[] data) throws IOException
090        {
091            return receive(data, data.length);
092        }
093    
094    }
095