SocketIO---bio1

1. 先启动服务端, 后启动客户端

 1 public class Server {
 2     public static final int port = 8765;
 3     
 4     public static void main(String[] args) {
 5         System.out.println("Server start...\n");
 6         Server server = new Server();
 7         server.init();
 8 
 9     }
10 
11     public void init()  {
12         try {
13             ServerSocket serverSocket = new ServerSocket(port); //监听端口
14             while(true){
15                 Socket socket = serverSocket.accept(); //接收客户端的请求数据
16                 new Thread(new HandlerThread(socket)).start(); //线程去处理请求
17             }
18         } catch (IOException e) {
19             System.out.println("服务器异常: " + e.getMessage());
20         }
21     }
22 }
View Code

2. 处理请求的线程

 1 public class ServerHandler implements Runnable{
 2     private Socket socket ;
 3     
 4     public ServerHandler(Socket socket){
 5         this.socket = socket;
 6     }
 7     
 8     @Override
 9     public void run() {
10         BufferedReader in = null;
11         PrintWriter out = null;
12         try {
13             in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));//接收客户端的请求数据
14             out = new PrintWriter(this.socket.getOutputStream(), true); //响应数据给客户端
15             String body = null;
16             while(true){
17                 body = in.readLine();
18                 if(body == null) break;
19                 System.out.println("Server :" + body);
20                 out.println("服务器端回送响的应数据.");
21             }
22             
23         } catch (Exception e) {
24             e.printStackTrace();
25         } finally {
26             if(in != null){
27                 try {
28                     in.close();
29                 } catch (IOException e) {
30                     e.printStackTrace();
31                 }
32             }
33             if(out != null){
34                 try {
35                     out.close();
36                 } catch (Exception e) {
37                     e.printStackTrace();
38                 }
39             }
40             if(socket != null){
41                 try {
42                     socket.close();
43                 } catch (IOException e) {
44                     e.printStackTrace();
45                 }
46             }
47             socket = null;
48         }
49         
50         
51     }
52 
53 }
View Code

3. 客户端

 1 public class Client {
 2 
 3     private static final int port = 8765;
 4     //private static final String host = "localhost"; 可以
 5     //private static final String host = "127.0.0.1"; 可以
 6     private static final String host = "192.168.233.1"; //可以
 7     
 8     public static void main(String[] args) {
 9         System.out.println("Client start...");
10         while(true){
11             Socket socket = null;
12             try {
13                 socket = new Socket(host, port);
14                 BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
15                 PrintStream out = new PrintStream(socket.getOutputStream());
16                 System.out.println("客户端请输入请求:\t");
17                 String str = new BufferedReader(new InputStreamReader(System.in)).readLine();
18                 out.println(str);
19                 
20                 String result = input.readLine();
21                 System.out.println("客户端接收到服务器端响应的数据:" + result);
22                 if("ok".equalsIgnoreCase(result)){
23                     System.out.println("客户端将要关闭连接");
24                     Thread.sleep(500);
25                     break;
26                 }
27                 out.close();
28                 input.close();
29                 
30             } catch (Exception e) {
31                 System.out.println("客户端异常:" + e.getMessage());
32             } finally {
33                 if(null != socket){
34                     try {
35                         socket.close();
36                     } catch (IOException e) {
37                         socket = null;
38                         System.out.println("客户端finally异常:"+e);
39                     }
40                 }
41             }
42         }
43     }
44 }
View Code

 

posted @ 2017-12-13 22:25  黑土白云  阅读(245)  评论(0编辑  收藏  举报