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