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