//***** try cesium.clock.org as host import java.net.Socket; import java.io.IOException; import java.io.InputStream; public class DayTime { public static void main(String[ ] args) { if (args.length < 1) { System.err.println("Usage: DayTime "); return; } try { // Port 13 is the well known for daytime Socket sock = new Socket(args[0], 13); // Get the socket's associated input stream InputStream in = sock.getInputStream(); // Read the bytes sent by remote host. byte[ ] bytes = new byte[256]; int next_byte = -1; int i = 0; while ((next_byte = in.read()) != -1 && i < bytes.length) bytes[i++] = (byte) next_byte; // Close the socket, which closes the input stream. sock.close(); // Print the bytes separately and as a string. int n = i; System.out.println("\nThe bytes:"); for (i = 0; i < n; i++) System.out.print(bytes[i] + " "); System.out.println("\n\nBytes as a string:\n" + new String(bytes)); } // UnknownHostException extends IOException catch(IOException e) { System.err.println(e); } } } /* Output from a sample run: The bytes: 70 114 105 32 77 97 114 32 50 56 32 49 50 58 51 54 58 52 57 32 50 48 48 51 13 10 The bytes as string characters: (if the server sent ASCII bytes). Fri Mar 28 12:36:49 2003\r\n */ //**********************************************************************************