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 DiscardUDPClient class is a UDP implementation of a client for the
  22. * Discard protocol described in RFC 863. 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. * After you're done sending discard data, call
  27. * <a href="org.apache.commons.net.DatagramSocketClient.html#close"> close() </a>
  28. * to clean up properly.
  29. * <p>
  30. * <p>
  31. * @author Daniel F. Savarese
  32. * @see DiscardTCPClient
  33. ***/
  34. public class DiscardUDPClient extends DatagramSocketClient
  35. {
  36. /*** The default discard port. It is set to 9 according to RFC 863. ***/
  37. public static final int DEFAULT_PORT = 9;
  38. DatagramPacket _sendPacket;
  39. public DiscardUDPClient()
  40. {
  41. _sendPacket = new DatagramPacket(new byte[0], 0);
  42. }
  43. /***
  44. * Sends the specified data to the specified server at the specified port.
  45. * <p>
  46. * @param data The discard data to send.
  47. * @param length The length of the data to send. Should be less than
  48. * or equal to the length of the data byte array.
  49. * @param host The address of the server.
  50. * @param port The service port.
  51. * @exception IOException If an error occurs during the datagram send
  52. * operation.
  53. ***/
  54. public void send(byte[] data, int length, InetAddress host, int port)
  55. throws IOException
  56. {
  57. _sendPacket.setData(data);
  58. _sendPacket.setLength(length);
  59. _sendPacket.setAddress(host);
  60. _sendPacket.setPort(port);
  61. _socket_.send(_sendPacket);
  62. }
  63. /***
  64. * Same as
  65. * <code>send(data, length, host. DiscardUDPClient.DEFAULT_PORT)</code>.
  66. ***/
  67. public void send(byte[] data, int length, InetAddress host)
  68. throws IOException
  69. {
  70. send(data, length, host, DEFAULT_PORT);
  71. }
  72. /***
  73. * Same as
  74. * <code>send(data, data.length, host. DiscardUDPClient.DEFAULT_PORT)</code>.
  75. ***/
  76. public void send(byte[] data, InetAddress host) throws IOException
  77. {
  78. send(data, data.length, host, DEFAULT_PORT);
  79. }
  80. }