View Javadoc

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  
18  package org.apache.commons.net.echo;
19  
20  import java.io.IOException;
21  import java.net.DatagramPacket;
22  import java.net.InetAddress;
23  
24  import org.apache.commons.net.discard.DiscardUDPClient;
25  
26  /***
27   * The EchoUDPClient class is a UDP implementation of a client for the
28   * Echo protocol described in RFC 862.  To use the class,
29   * just open a local UDP port
30   * with {@link org.apache.commons.net.DatagramSocketClient#open  open }
31   * and call {@link #send  send } to send datagrams to the server,
32   * then call {@link #receive  receive } to receive echoes.
33   * After you're done echoing data, call
34   * {@link org.apache.commons.net.DatagramSocketClient#close  close() }
35   * to clean up properly.
36   * <p>
37   * <p>
38   * @see EchoTCPClient
39   * @see DiscardUDPClient
40   ***/
41  
42  public final class EchoUDPClient extends DiscardUDPClient
43  {
44      /*** The default echo port.  It is set to 7 according to RFC 862. ***/
45      public static final int DEFAULT_PORT = 7;
46  
47      private final DatagramPacket __receivePacket = new DatagramPacket(new byte[0], 0);
48  
49      /***
50       * Sends the specified data to the specified server at the default echo
51       * port.
52       * <p>
53       * @param data  The echo data to send.
54       * @param length  The length of the data to send.  Should be less than
55       *    or equal to the length of the data byte array.
56       * @param host  The address of the server.
57       * @exception IOException If an error occurs during the datagram send
58       *     operation.
59       ***/
60      @Override
61      public void send(byte[] data, int length, InetAddress host)
62      throws IOException
63      {
64          send(data, length, host, DEFAULT_PORT);
65      }
66  
67  
68      /*** Same as <code> send(data, data.length, host) </code> ***/
69      @Override
70      public void send(byte[] data, InetAddress host) throws IOException
71      {
72          send(data, data.length, host, DEFAULT_PORT);
73      }
74  
75  
76      /***
77       * Receives echoed data and returns its length.  The data may be divided
78       * up among multiple datagrams, requiring multiple calls to receive.
79       * Also, the UDP packets will not necessarily arrive in the same order
80       * they were sent.
81       * <p>
82       * @return  Length of actual data received.
83       * @exception IOException If an error occurs while receiving the data.
84       ***/
85      public int receive(byte[] data, int length) throws IOException
86      {
87          __receivePacket.setData(data);
88          __receivePacket.setLength(length);
89          _socket_.receive(__receivePacket);
90          return __receivePacket.getLength();
91      }
92  
93      /*** Same as <code> receive(data, data.length)</code> ***/
94      public int receive(byte[] data) throws IOException
95      {
96          return receive(data, data.length);
97      }
98  
99  }
100