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