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