Java基础网络---1-----连接到服务器

      编写客户端,链接到指定服务器。

 

 1 package com.socket;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.net.Socket;
 6 import java.util.Scanner;
 7 
 8 public class SoketTest {
 9 
10     public static void main(String[] args) throws IOException {
11         // TODO Auto-generated method stub
12         //使用try获取资源,该资源被自动回收
13         //Socket(String host,int port)构建套接字,指定主机和端口号
14         try(Socket client = new Socket("time-A.timefep.gov",13)){
15             InputStream in = client.getInputStream();//获取输入流
16             Scanner sc = new Scanner(in);
17             while(sc.hasNext()){
18                 System.out.println(sc.nextLine());
19             }
20             sc.close();
21         }
22 
23     }
24 
25 }

 

posted on 2017-07-15 19:20  笑傲江湖未平生  阅读(173)  评论(0)    收藏  举报