DaytimeTCPClient.java

  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. package org.apache.commons.net.daytime;

  18. import java.io.IOException;

  19. import org.apache.commons.io.IOUtils;
  20. import org.apache.commons.net.SocketClient;

  21. /**
  22.  * 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
  23.  * with {@link org.apache.commons.net.SocketClient#connect connect} and call {@link #getTime getTime()} to retrieve the daytime string, then call
  24.  * {@link org.apache.commons.net.SocketClient#disconnect disconnect} to close the connection properly.
  25.  *
  26.  * @see DaytimeUDPClient
  27.  */
  28. public final class DaytimeTCPClient extends SocketClient {

  29.     /**
  30.      * The default daytime port {@value} per RFC 867.
  31.      */
  32.     public static final int DEFAULT_PORT = 13;

  33.     /**
  34.      * The default DaytimeTCPClient constructor. It merely sets the default port to <code>DEFAULT_PORT</code>.
  35.      */
  36.     public DaytimeTCPClient() {
  37.         setDefaultPort(DEFAULT_PORT);
  38.     }

  39.     /**
  40.      * Gets the time string from the server and returns it. The server will have closed the connection at this point, so you should call
  41.      * {@link org.apache.commons.net.SocketClient#disconnect disconnect} after calling this method. To retrieve another time, you must initiate another
  42.      * connection with {@link org.apache.commons.net.SocketClient#connect connect} before calling {@code getTime()} again.
  43.      *
  44.      * @return The time string retrieved from the server.
  45.      * @throws IOException If an error occurs while fetching the time string.
  46.      */
  47.     public String getTime() throws IOException {
  48.         return IOUtils.toString(_input_, getCharset());
  49.     }

  50. }