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.BufferedReader;
  18. import java.io.IOException;
  19. import java.io.InputStreamReader;
  20. /***
  21. * The DaytimeTCPClient class is a TCP implementation of a client for the
  22. * Daytime protocol described in RFC 867. To use the class, merely
  23. * establish a connection with
  24. * <a href="org.apache.commons.net.SocketClient.html#connect"> connect </a>
  25. * and call <a href="#getTime"> getTime() </a> to retrieve the daytime
  26. * string, then
  27. * call <a href="org.apache.commons.net.SocketClient.html#disconnect"> disconnect </a>
  28. * to close the connection properly.
  29. * <p>
  30. * <p>
  31. * @author Daniel F. Savarese
  32. * @see DaytimeUDPClient
  33. ***/
  34. public final class DaytimeTCPClient extends SocketClient
  35. {
  36. /*** The default daytime port. It is set to 13 according to RFC 867. ***/
  37. public static final int DEFAULT_PORT = 13;
  38. // Received dates will likely be less than 64 characters.
  39. // This is a temporary buffer used while receiving data.
  40. private char[] __buffer = new char[64];
  41. /***
  42. * The default DaytimeTCPClient constructor. It merely sets the default
  43. * port to <code> DEFAULT_PORT </code>.
  44. ***/
  45. public DaytimeTCPClient ()
  46. {
  47. setDefaultPort(DEFAULT_PORT);
  48. }
  49. /***
  50. * Retrieves the time string from the server and returns it. The
  51. * server will have closed the connection at this point, so you should
  52. * call
  53. * <a href="org.apache.commons.net.SocketClient.html#disconnect"> disconnect </a>
  54. * after calling this method. To retrieve another time, you must
  55. * initiate another connection with
  56. * <a href="org.apache.commons.net.SocketClient.html#connect"> connect </a>
  57. * before calling <code> getTime() </code> again.
  58. * <p>
  59. * @return The time string retrieved from the server.
  60. * @exception IOException If an error occurs while fetching the time string.
  61. ***/
  62. public String getTime() throws IOException
  63. {
  64. int read;
  65. StringBuffer result = new StringBuffer(__buffer.length);
  66. BufferedReader reader;
  67. reader = new BufferedReader(new InputStreamReader(_input_));
  68. while (true)
  69. {
  70. read = reader.read(__buffer, 0, __buffer.length);
  71. if (read <= 0)
  72. break;
  73. result.append(__buffer, 0, read);
  74. }
  75. return result.toString();
  76. }
  77. }