View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.net;
18  
19  import java.io.BufferedReader;
20  import java.io.IOException;
21  import java.io.InputStreamReader;
22  
23  /***
24   * The DaytimeTCPClient class is a TCP implementation of a client for the
25   * Daytime protocol described in RFC 867.  To use the class, merely
26   * establish a connection with
27   * {@link org.apache.commons.net.SocketClient#connect  connect }
28   * and call {@link #getTime  getTime() } to retrieve the daytime
29   * string, then
30   * call {@link org.apache.commons.net.SocketClient#disconnect  disconnect }
31   * to close the connection properly.
32   * <p>
33   * <p>
34   * @author Daniel F. Savarese
35   * @see DaytimeUDPClient
36   ***/
37  
38  public final class DaytimeTCPClient extends SocketClient
39  {
40      /*** The default daytime port.  It is set to 13 according to RFC 867. ***/
41      public static final int DEFAULT_PORT = 13;
42  
43      // Received dates will likely be less than 64 characters.
44      // This is a temporary buffer used while receiving data.
45      private char[] __buffer = new char[64];
46  
47      /***
48       * The default DaytimeTCPClient constructor.  It merely sets the default
49       * port to <code> DEFAULT_PORT </code>.
50       ***/
51      public DaytimeTCPClient ()
52      {
53          setDefaultPort(DEFAULT_PORT);
54      }
55  
56      /***
57       * Retrieves the time string from the server and returns it.  The
58       * server will have closed the connection at this point, so you should
59       * call
60       * {@link org.apache.commons.net.SocketClient#disconnect  disconnect }
61       * after calling this method.  To retrieve another time, you must
62       * initiate another connection with
63       * {@link org.apache.commons.net.SocketClient#connect  connect }
64       * before calling <code> getTime() </code> again.
65       * <p>
66       * @return The time string retrieved from the server.
67       * @exception IOException  If an error occurs while fetching the time string.
68       ***/
69      public String getTime() throws IOException
70      {
71          int read;
72          StringBuffer result = new StringBuffer(__buffer.length);
73          BufferedReader reader;
74  
75          reader = new BufferedReader(new InputStreamReader(_input_));
76  
77          while (true)
78          {
79              read = reader.read(__buffer, 0, __buffer.length);
80              if (read <= 0)
81                  break;
82              result.append(__buffer, 0, read);
83          }
84  
85          return result.toString();
86      }
87  
88  }
89