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    *      https://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  
18  package org.apache.commons.net.daytime;
19  
20  import java.io.IOException;
21  
22  import org.apache.commons.io.IOUtils;
23  import org.apache.commons.net.SocketClient;
24  
25  /**
26   * The DaytimeTCPClient class is a TCP implementation of a client for the Daytime protocol described in RFC 867. To use the class, merely establish a connection
27   * with {@link org.apache.commons.net.SocketClient#connect connect} and call {@link #getTime getTime()} to retrieve the daytime string, then call
28   * {@link org.apache.commons.net.SocketClient#disconnect disconnect} to close the connection properly.
29   *
30   * @see DaytimeUDPClient
31   */
32  public final class DaytimeTCPClient extends SocketClient {
33  
34      /**
35       * The default daytime port {@value} per RFC 867.
36       */
37      public static final int DEFAULT_PORT = 13;
38  
39      /**
40       * The default DaytimeTCPClient constructor. It merely sets the default port to <code>DEFAULT_PORT</code>.
41       */
42      public DaytimeTCPClient() {
43          setDefaultPort(DEFAULT_PORT);
44      }
45  
46      /**
47       * Gets the time string from the server and returns it. The server will have closed the connection at this point, so you should call
48       * {@link org.apache.commons.net.SocketClient#disconnect disconnect} after calling this method. To retrieve another time, you must initiate another
49       * connection with {@link org.apache.commons.net.SocketClient#connect connect} before calling {@code getTime()} again.
50       *
51       * @return The time string retrieved from the server.
52       * @throws IOException If an error occurs while fetching the time string.
53       */
54      public String getTime() throws IOException {
55          return IOUtils.toString(_input_, getCharset());
56      }
57  
58  }