The Java Program: NetworkTime.java
1
2
3 import java.net.Socket;
4 import java.net.UnknownHostException;
5 import java.io.PrintWriter;
6 import java.io.BufferedReader;
7 import java.io.InputStreamReader;
8 import java.io.IOException;
9
10 public class NetworkTime {
11
12 final static int port = 13;
13
14 public static void main (String [] args) {
15
16 final String hostname = args.length>0?args[0]:"nist1.columbiacountyga.gov";
17
18 try {
19 final Socket so = new Socket (hostname, port);
20 final PrintWriter pw = new PrintWriter (so.getOutputStream());
21 pw.println();
22 final BufferedReader in = new BufferedReader (
23 new InputStreamReader (so.getInputStream()));
24
25 final String time = in.readLine();
26 System.out.println (time.length());
27 System.out.println (so.getInetAddress() + " says it is " + time);
28
29 so.close();
30 } catch (IOException e) {
31 e.printStackTrace (System.err);
32 }
33 }
34 }