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 029 * Echo protocol described in RFC 862. To use the class, 030 * just open a local UDP port 031 * with {@link org.apache.commons.net.DatagramSocketClient#open open } 032 * and call {@link #send send } to send datagrams to the server, 033 * then call {@link #receive receive } to receive echoes. 034 * After you're done echoing data, call 035 * {@link org.apache.commons.net.DatagramSocketClient#close close() } 036 * to clean up properly. 037 * 038 * @see EchoTCPClient 039 * @see DiscardUDPClient 040 */ 041 042public 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(NetConstants.EMPTY_BTYE_ARRAY, 0); 048 049 /** 050 * Sends the specified data to the specified server at the default echo 051 * port. 052 * 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 * @throws IOException If an error occurs during the datagram send 058 * operation. 059 */ 060 @Override 061 public void send(final byte[] data, final int length, final 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(final byte[] data, final 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 * @param data the buffer to receive the input 082 * @param length of the buffer 083 * 084 * @return Length of actual data received. 085 * @throws IOException If an error occurs while receiving the data. 086 */ 087 public int receive(final byte[] data, final int length) throws IOException 088 { 089 receivePacket.setData(data); 090 receivePacket.setLength(length); 091 _socket_.receive(receivePacket); 092 return receivePacket.getLength(); 093 } 094 095 /** Same as <code> receive(data, data.length)</code> 096 * @param data the buffer to receive the input 097 * @return the number of bytes 098 * @throws IOException on error 099 */ 100 public int receive(final byte[] data) throws IOException 101 { 102 return receive(data, data.length); 103 } 104 105} 106