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 DaytimeUDPClient class is a UDP implementation of a client for the
  22. * Daytime protocol described in RFC 867. To use the class, merely
  23. * open a local datagram socket with
  24. * <a href="org.apache.commons.net.DatagramSocketClient.html#open"> open </a>
  25. * and call <a href="#getTime"> getTime </a> to retrieve the daytime
  26. * string, then
  27. * call <a href="org.apache.commons.net.DatagramSocketClient.html#close"> close </a>
  28. * to close the connection properly. Unlike
  29. * <a href="org.apache.commons.net.DaytimeTCPClient.html"> DaytimeTCPClient </a>,
  30. * successive calls to <a href="#getTime"> getTime </a> are permitted
  31. * without re-establishing a connection. That is because UDP is a
  32. * connectionless protocol and the Daytime protocol is stateless.
  33. * <p>
  34. * <p>
  35. * @author Daniel F. Savarese
  36. * @see DaytimeTCPClient
  37. ***/
  38. public final class DaytimeUDPClient extends DatagramSocketClient
  39. {
  40. /*** The default daytime port. It is set to 13 according to RFC 867. ***/
  41. public static final int DEFAULT_PORT = 13;
  42. private byte[] __dummyData = new byte[1];
  43. // Received dates should be less than 256 bytes
  44. private byte[] __timeData = new byte[256];
  45. /***
  46. * Retrieves the time string from the specified server and port and
  47. * returns it.
  48. * <p>
  49. * @param host The address of the server.
  50. * @param port The port of the service.
  51. * @return The time string.
  52. * @exception IOException If an error occurs while retrieving the time.
  53. ***/
  54. public String getTime(InetAddress host, int port) throws IOException
  55. {
  56. DatagramPacket sendPacket, receivePacket;
  57. sendPacket =
  58. new DatagramPacket(__dummyData, __dummyData.length, host, port);
  59. receivePacket = new DatagramPacket(__timeData, __timeData.length);
  60. _socket_.send(sendPacket);
  61. _socket_.receive(receivePacket);
  62. return new String(receivePacket.getData(), 0, receivePacket.getLength());
  63. }
  64. /*** Same as <code>getTime(host, DaytimeUDPClient.DEFAULT_PORT);</code> ***/
  65. public String getTime(InetAddress host) throws IOException
  66. {
  67. return getTime(host, DEFAULT_PORT);
  68. }
  69. }