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 CharGenUDPClient class is a UDP implementation of a client for the
  22. * character generator protocol described in RFC 864. It can also be
  23. * used for Systat (RFC 866), Quote of the Day (RFC 865), and netstat
  24. * (port 15). All of these protocols involve sending a datagram to the
  25. * appropriate port, and reading data contained in one or more reply
  26. * datagrams. The chargen and quote of the day protocols only send
  27. * one reply datagram containing 512 bytes or less of data. The other
  28. * protocols may reply with more than one datagram, in which case you
  29. * must wait for a timeout to determine that all reply datagrams have
  30. * been sent.
  31. * <p>
  32. * To use the CharGenUDPClient class, just open a local UDP port
  33. * with <a href="org.apache.commons.net.DatagramSocketClient.html#open"> open </a>
  34. * and call <a href="#send"> send </a> to send the datagram that will
  35. * initiate the data reply. For chargen or quote of the day, just
  36. * call <a href="#recieve"> receive </a>, and you're done. For netstat and
  37. * systat, call receive in a while loop, and catch a SocketException and
  38. * InterruptedIOException to detect a timeout (don't forget to set the
  39. * timeout duration beforehand). Don't forget to call
  40. * <a href="org.apache.commons.net.DatagramSocketClient.html#close"> close() </a>
  41. * to clean up properly.
  42. * <p>
  43. * <p>
  44. * @author Daniel F. Savarese
  45. * @see CharGenTCPClient
  46. ***/
  47. public final class CharGenUDPClient extends DatagramSocketClient
  48. {
  49. /*** The systat port value of 11 according to RFC 866. ***/
  50. public static final int SYSTAT_PORT = 11;
  51. /*** The netstat port value of 19. ***/
  52. public static final int NETSTAT_PORT = 15;
  53. /*** The quote of the day port value of 17 according to RFC 865. ***/
  54. public static final int QUOTE_OF_DAY_PORT = 17;
  55. /*** The character generator port value of 19 according to RFC 864. ***/
  56. public static final int CHARGEN_PORT = 19;
  57. /*** The default chargen port. It is set to 19 according to RFC 864. ***/
  58. public static final int DEFAULT_PORT = 19;
  59. private byte[] __receiveData;
  60. private DatagramPacket __receivePacket;
  61. private DatagramPacket __sendPacket;
  62. /***
  63. * The default CharGenUDPClient constructor. It initializes some internal
  64. * data structures for sending and receiving the necessary datagrams for
  65. * the chargen and related protocols.
  66. ***/
  67. public CharGenUDPClient()
  68. {
  69. // CharGen return packets have a maximum length of 512
  70. __receiveData = new byte[512];
  71. __receivePacket = new DatagramPacket(__receiveData, 512);
  72. __sendPacket = new DatagramPacket(new byte[0], 0);
  73. }
  74. /***
  75. * Sends the data initiation datagram. This data in the packet is ignored
  76. * by the server, and merely serves to signal that the server should send
  77. * its reply.
  78. * <p>
  79. * @param host The address of the server.
  80. * @param port The port of the service.
  81. * @exception IOException If an error occurs while sending the datagram.
  82. ***/
  83. public void send(InetAddress host, int port) throws IOException
  84. {
  85. __sendPacket.setAddress(host);
  86. __sendPacket.setPort(port);
  87. _socket_.send(__sendPacket);
  88. }
  89. /*** Same as <code>send(host, CharGenUDPClient.DEFAULT_PORT);</code> ***/
  90. public void send(InetAddress host) throws IOException
  91. {
  92. send(host, DEFAULT_PORT);
  93. }
  94. /***
  95. * Receive the reply data from the server. This will always be 512 bytes
  96. * or less. Chargen and quote of the day only return one packet. Netstat
  97. * and systat require multiple calls to receive() with timeout detection.
  98. * <p>
  99. * @return The reply data from the server.
  100. * @exception IOException If an error occurs while receiving the datagram.
  101. ***/
  102. public byte[] receive() throws IOException
  103. {
  104. int length;
  105. byte[] result;
  106. _socket_.receive(__receivePacket);
  107. result = new byte[length = __receivePacket.getLength()];
  108. System.arraycopy(__receiveData, 0, result, 0, length);
  109. return result;
  110. }
  111. }