1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.net;
18
19 import java.io.IOException;
20 import java.net.DatagramPacket;
21 import java.net.InetAddress;
22
23 /***
24 * The EchoUDPClient class is a UDP implementation of a client for the
25 * Echo protocol described in RFC 862. To use the class,
26 * just open a local UDP port
27 * with {@link org.apache.commons.net.DatagramSocketClient#open open }
28 * and call {@link #send send } to send datagrams to the server,
29 * then call {@link #receive receive } to receive echoes.
30 * After you're done echoing data, call
31 * {@link org.apache.commons.net.DatagramSocketClient#close close() }
32 * to clean up properly.
33 * <p>
34 * <p>
35 * @author Daniel F. Savarese
36 * @see EchoTCPClient
37 * @see DiscardUDPClient
38 ***/
39
40 public final class EchoUDPClient extends DiscardUDPClient
41 {
42 /*** The default echo port. It is set to 7 according to RFC 862. ***/
43 public static final int DEFAULT_PORT = 7;
44
45 private DatagramPacket __receivePacket = new DatagramPacket(new byte[0], 0);
46
47 /***
48 * Sends the specified data to the specified server at the default echo
49 * port.
50 * <p>
51 * @param data The echo data to send.
52 * @param length The length of the data to send. Should be less than
53 * or equal to the length of the data byte array.
54 * @param host The address of the server.
55 * @exception IOException If an error occurs during the datagram send
56 * operation.
57 ***/
58 public void send(byte[] data, int length, InetAddress host)
59 throws IOException
60 {
61 send(data, length, host, DEFAULT_PORT);
62 }
63
64
65 /*** Same as <code> send(data, data.length, host) </code> ***/
66 public void send(byte[] data, InetAddress host) throws IOException
67 {
68 send(data, data.length, host, DEFAULT_PORT);
69 }
70
71
72 /***
73 * Receives echoed data and returns its length. The data may be divided
74 * up among multiple datagrams, requiring multiple calls to receive.
75 * Also, the UDP packets will not necessarily arrive in the same order
76 * they were sent.
77 * <p>
78 * @return Length of actual data received.
79 * @exception IOException If an error occurs while receiving the data.
80 ***/
81 public int receive(byte[] data, int length) throws IOException
82 {
83 __receivePacket.setData(data);
84 __receivePacket.setLength(length);
85 _socket_.receive(__receivePacket);
86 return __receivePacket.getLength();
87 }
88
89 /*** Same as <code> receive(data, data.length)</code> ***/
90 public int receive(byte[] data) throws IOException
91 {
92 return receive(data, data.length);
93 }
94
95 }
96