获取在线时间 (Java Socket)

 1 import java.io.IOException;
 2 import java.io.InputStream;
 3 import java.net.Socket;
 4 import java.util.Scanner;
 5 
 6 
 7 public class SocketTest {
 8     public static void main(String[] args) {
 9         Socket s = null;
10         try {
11             s = new Socket("time-A.timefreq.bldrdoc.gov",13);
12             InputStream inStream = s.getInputStream();
13             Scanner in = new Scanner(inStream);
14             
15             while(in.hasNextLine()) {
16                 String line = in.nextLine();
17                 System.out.println(line);
18             }
19             
20         } catch (Exception ex) {
21             ex.printStackTrace();
22         } 
23         finally {
24             if (s != null) {
25                 try {
26                     s.close();
27                 } catch (IOException e) {
28                     // TODO Auto-generated catch block
29                     e.printStackTrace();
30                 }
31             }
32         }
33     }
34 }
输出格式为:56252 12-11-21 03:48:11 00 0 0 647.2 UTC(NIST) *   红色加8就行了,因为区域的问题
posted @ 2012-11-21 11:51  易木  阅读(526)  评论(0编辑  收藏  举报