01: //***** try cesium.clock.org as host
02: import java.net.Socket;
03: import java.io.IOException;
04: import java.io.InputStream;
05: public class DayTime {
06:     public static void main(String[ ] args) {
07:         if (args.length < 1) {
08:            System.err.println("Usage: DayTime <IP address>");
09:            return;
10:         }
11:         try {
12:             // Port 13 is the well known for daytime
13:             Socket sock = new Socket(args[0], 13);
14: 
15:             // Get the socket's associated input stream
16:             InputStream in = sock.getInputStream();
17: 
18:             // Read the bytes sent by remote host.
19:             byte[ ] bytes = new byte[256];  
20:             int next_byte = -1;
21:             int i = 0;
22:             while ((next_byte = in.read()) != -1 && i < bytes.length) 
23:                 bytes[i++] = (byte) next_byte;
24:             // Close the socket, which closes the input stream.
25:             sock.close();
26: 
27:             // Print the bytes separately and as a string.
28:             int n = i;
29:             System.out.println("\nThe bytes:");
30:             for (i = 0; i < n; i++) 
31:                 System.out.print(bytes[i] + " ");
32:             System.out.println("\n\nBytes as a string:\n" 
33:                 + new String(bytes));
34:         }
35:         // UnknownHostException extends IOException
36:         catch(IOException e) {
37:             System.err.println(e);
38:         }
39:     }
40: }
41: /* Output from a sample run: 
42: The bytes:
43: 70 114 105 32 77 97 114 32 50 56 32 49 50 58 51 54 58 52 57 32 50 48 48 51 
44: 13 10 
45: 
46: The bytes as string characters: (if the server sent ASCII bytes).
47: Fri Mar 28 12:36:49 2003\r\n
48: */
49: //**********************************************************************************