001 /* 002 * Copyright 2001-2005 The Apache Software Foundation 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.apache.commons.net; 017 018 import java.io.BufferedReader; 019 import java.io.IOException; 020 import java.io.InputStreamReader; 021 022 /*** 023 * The DaytimeTCPClient class is a TCP implementation of a client for the 024 * Daytime protocol described in RFC 867. To use the class, merely 025 * establish a connection with 026 * {@link org.apache.commons.net.SocketClient#connect connect } 027 * and call {@link #getTime getTime() } to retrieve the daytime 028 * string, then 029 * call {@link org.apache.commons.net.SocketClient#disconnect disconnect } 030 * to close the connection properly. 031 * <p> 032 * <p> 033 * @author Daniel F. Savarese 034 * @see DaytimeUDPClient 035 ***/ 036 037 public final class DaytimeTCPClient extends SocketClient 038 { 039 /*** The default daytime port. It is set to 13 according to RFC 867. ***/ 040 public static final int DEFAULT_PORT = 13; 041 042 // Received dates will likely be less than 64 characters. 043 // This is a temporary buffer used while receiving data. 044 private char[] __buffer = new char[64]; 045 046 /*** 047 * The default DaytimeTCPClient constructor. It merely sets the default 048 * port to <code> DEFAULT_PORT </code>. 049 ***/ 050 public DaytimeTCPClient () 051 { 052 setDefaultPort(DEFAULT_PORT); 053 } 054 055 /*** 056 * Retrieves the time string from the server and returns it. The 057 * server will have closed the connection at this point, so you should 058 * call 059 * {@link org.apache.commons.net.SocketClient#disconnect disconnect } 060 * after calling this method. To retrieve another time, you must 061 * initiate another connection with 062 * {@link org.apache.commons.net.SocketClient#connect connect } 063 * before calling <code> getTime() </code> again. 064 * <p> 065 * @return The time string retrieved from the server. 066 * @exception IOException If an error occurs while fetching the time string. 067 ***/ 068 public String getTime() throws IOException 069 { 070 int read; 071 StringBuffer result = new StringBuffer(__buffer.length); 072 BufferedReader reader; 073 074 reader = new BufferedReader(new InputStreamReader(_input_)); 075 076 while (true) 077 { 078 read = reader.read(__buffer, 0, __buffer.length); 079 if (read <= 0) 080 break; 081 result.append(__buffer, 0, read); 082 } 083 084 return result.toString(); 085 } 086 087 } 088