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. import java.util.Date;
  21. /***
  22. * The TimeUDPClient class is a UDP implementation of a client for the
  23. * Time protocol described in RFC 868. To use the class, merely
  24. * open a local datagram socket with
  25. * <a href="org.apache.commons.net.DatagramSocketClient.html#open"> open </a>
  26. * and call <a href="#getTime"> getTime </a> or
  27. * <a href="#getTime"> getDate </a> to retrieve the time. Then call
  28. * <a href="org.apache.commons.net.DatagramSocketClient.html#close"> close </a>
  29. * to close the connection properly. Unlike
  30. * <a href="org.apache.commons.net.TimeTCPClient.html"> TimeTCPClient </a>,
  31. * successive calls to <a href="#getTime"> getTime </a> or
  32. * <a href="#getDate"> getDate </a> are permitted
  33. * without re-establishing a connection. That is because UDP is a
  34. * connectionless protocol and the Time protocol is stateless.
  35. * <p>
  36. * <p>
  37. * @author Daniel F. Savarese
  38. * @see TimeTCPClient
  39. ***/
  40. public final class TimeUDPClient extends DatagramSocketClient
  41. {
  42. /*** The default time port. It is set to 37 according to RFC 868. ***/
  43. public static final int DEFAULT_PORT = 37;
  44. /***
  45. * The number of seconds between 00:00 1 January 1900 and
  46. * 00:00 1 January 1970. This value can be useful for converting
  47. * time values to other formats.
  48. ***/
  49. public static final long SECONDS_1900_TO_1970 = 2208988800L;
  50. private byte[] __dummyData = new byte[1];
  51. private byte[] __timeData = new byte[4];
  52. /***
  53. * Retrieves the time from the specified server and port and
  54. * returns it. The time is the number of seconds since
  55. * 00:00 (midnight) 1 January 1900 GMT, as specified by RFC 868.
  56. * This method reads the raw 32-bit big-endian
  57. * unsigned integer from the server, converts it to a Java long, and
  58. * returns the value.
  59. * <p>
  60. * @param host The address of the server.
  61. * @param port The port of the service.
  62. * @return The time value retrieved from the server.
  63. * @exception IOException If an error occurs while retrieving the time.
  64. ***/
  65. public long getTime(InetAddress host, int port) throws IOException
  66. {
  67. long time;
  68. DatagramPacket sendPacket, receivePacket;
  69. sendPacket =
  70. new DatagramPacket(__dummyData, __dummyData.length, host, port);
  71. receivePacket = new DatagramPacket(__timeData, __timeData.length);
  72. _socket_.send(sendPacket);
  73. _socket_.receive(receivePacket);
  74. time = 0L;
  75. time |= (((__timeData[0] & 0xff) << 24) & 0xffffffffL);
  76. time |= (((__timeData[1] & 0xff) << 16) & 0xffffffffL);
  77. time |= (((__timeData[2] & 0xff) << 8) & 0xffffffffL);
  78. time |= ((__timeData[3] & 0xff) & 0xffffffffL);
  79. return time;
  80. }
  81. /*** Same as <code> getTime(host, DEFAULT_PORT); </code> ***/
  82. public long getTime(InetAddress host) throws IOException
  83. {
  84. return getTime(host, DEFAULT_PORT);
  85. }
  86. /***
  87. * Retrieves the time from the server and returns a Java Date
  88. * containing the time converted to the local timezone.
  89. * <p>
  90. * @param host The address of the server.
  91. * @param port The port of the service.
  92. * @return A Date value containing the time retrieved from the server
  93. * converted to the local timezone.
  94. * @exception IOException If an error occurs while fetching the time.
  95. ***/
  96. public Date getDate(InetAddress host, int port) throws IOException
  97. {
  98. return new Date((getTime(host, port) - SECONDS_1900_TO_1970)*1000L);
  99. }
  100. /*** Same as <code> getTime(host, DEFAULT_PORT); </code> ***/
  101. public Date getDate(InetAddress host) throws IOException
  102. {
  103. return new Date((getTime(host, DEFAULT_PORT) -
  104. SECONDS_1900_TO_1970)*1000L);
  105. }
  106. }