一、同步阻塞方式(BIO)
方式一:
服务器端(Server):
1 package com.ietree.basicskill.socket.mode1; 2 3 import java.io.IOException; 4 import java.net.ServerSocket; 5 import java.net.Socket; 6 7 /** 8 * 服务端 9 */ 10 public class Server { 11 // 端口号 12 final static int PORT = 8765; 13 public static void main(String[] args) { 14 ServerSocket server = null; 15 try { 16 server = new ServerSocket(PORT); 17 System.out.println("Server start......"); 18 // 进行阻塞 19 Socket socket = server.accept(); 20 // 创建一个程序执行客户端的任务 21 new Thread(new ServerHandler(socket)).start(); 22 23 } catch (IOException e) { 24 e.printStackTrace(); 25 } finally { 26 if(server != null){ 27 try { 28 server.close(); 29 } catch (IOException e) { 30 e.printStackTrace(); 31 } 32 } 33 server = null; 34 } 35 } 36 }
采用多线程来处理接收到的请求(ServerHandler):
1 package com.ietree.basicskill.socket.mode1; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.io.PrintWriter; 7 import java.net.Socket; 8 9 public class ServerHandler implements Runnable { 10 private Socket socket; 11 public ServerHandler(Socket socket) { 12 this.socket = socket; 13 } 14 15 @Override 16 public void run() { 17 BufferedReader in = null; 18 PrintWriter out = null; 19 try { 20 in = new BufferedReader(new InputStreamReader(this.socket.getInputStream())); 21 out = new PrintWriter(this.socket.getOutputStream(), true); 22 String body = null; 23 while (true) { 24 body = in.readLine(); 25 if(body == null){ 26 break; 27 } 28 System.out.println("Server: " + body); 29 out.println("服务器端回送响应的数据。"); 30 } 31 } catch (Exception e) { 32 e.printStackTrace(); 33 } finally { 34 if(in != null){ 35 try { 36 in.close(); 37 } catch (IOException e) { 38 e.printStackTrace(); 39 } 40 } 41 if(out != null){ 42 try { 43 out.close(); 44 } catch (Exception e) { 45 e.printStackTrace(); 46 } 47 } 48 if(socket != null){ 49 try { 50 socket.close(); 51 } catch (IOException e) { 52 e.printStackTrace(); 53 } 54 } 55 socket = null; 56 } 57 } 58 }
客户端(Client):
1 package com.ietree.basicskill.socket.mode1; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.io.PrintWriter; 7 import java.net.Socket; 8 9 /** 10 * 客户端 11 */ 12 public class Client { 13 final static String ADDRESS = "127.0.0.1"; 14 final static int PORT = 8765; 15 public static void main(String[] args) { 16 Socket socket = null; 17 BufferedReader in = null; 18 PrintWriter out = null; 19 try { 20 socket = new Socket(ADDRESS, PORT); 21 in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 22 out = new PrintWriter(socket.getOutputStream(), true); 23 24 // 向服务器端发送数据 25 out.println("接收到客户端的请求数据......"); 26 String response = in.readLine(); 27 System.out.println("Client: " + response); 28 } catch (Exception e) { 29 e.printStackTrace(); 30 } finally { 31 if(in != null){ 32 try { 33 in.close(); 34 } catch (IOException e) { 35 e.printStackTrace(); 36 } 37 } 38 if(out != null){ 39 try { 40 out.close(); 41 } catch (Exception e) { 42 e.printStackTrace(); 43 } 44 } 45 if(socket != null){ 46 try { 47 socket.close(); 48 } catch (IOException e) { 49 e.printStackTrace(); 50 } 51 } 52 socket = null; 53 } 54 } 55 }
程序输出:
Server:
Server start......
Server: 接收到客户端的请求数据......
Client:
Client: 服务器端回送响应的数据。
方式二:
Server类:
package com.ietree.basicskill.socket.basic.bio2; import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; /** * Created by Administrator on 2017/5/25. */ public class Server { final static int PORT = 8765; public static void main(String[] args) { ServerSocket server = null; BufferedReader in = null; PrintWriter out = null; try { server = new ServerSocket(PORT); System.out.println("Server Start......"); Socket socket = null; HandlerExecutorPool executorPool = new HandlerExecutorPool(50, 1000); while (true) { socket = server.accept(); executorPool.execute(new ServerHandler(socket)); } } catch (IOException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (Exception e1) { e1.printStackTrace(); } } if (out != null) { try { out.close(); } catch (Exception e2) { e2.printStackTrace(); } } if (server != null) { try { server.close(); } catch (Exception e3) { e3.printStackTrace(); } } server = null; } } }
serverHandler类:
package com.ietree.basicskill.socket.basic.bio2; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; /** * Created by Administrator on 2017/5/25. */ public class ServerHandler implements Runnable { private Socket socket; public ServerHandler(Socket socket) { this.socket = socket; } @Override public void run() { BufferedReader in = null; PrintWriter out = null; try { try { in = new BufferedReader(new InputStreamReader(this.socket.getInputStream())); out = new PrintWriter(this.socket.getOutputStream(), true); String body = null; while (true) { body = in.readLine(); if (body == null) { System.out.println("Server:" + body); out.println("Server Response"); } } } catch (IOException e) { e.printStackTrace(); } } finally { if (in != null) { try { in.close(); } catch (Exception e1) { e1.printStackTrace(); } } if (out != null) { try { out.close(); } catch (Exception e2) { e2.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (Exception e3) { e3.printStackTrace(); } } socket = null; } } }
线程池HandlerExecutorPool类:
package com.ietree.basicskill.socket.basic.bio2; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * Created by Administrator on 2017/5/25. */ public class HandlerExecutorPool { private ExecutorService executor; public HandlerExecutorPool(int maxPoolSize, int queueSize) { this.executor = new ThreadPoolExecutor( Runtime.getRuntime().availableProcessors(), maxPoolSize, 120L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(queueSize) ); } public void execute(Runnable task) { executor.execute(task); } }
客户端Client类:
package com.ietree.basicskill.socket.basic.bio2; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; /** * Created by Administrator on 2017/5/25. */ public class Client { final static String ADDRESS = "127.0.0.1"; final static int PORT = 8765; public static void main(String[] args) { Socket socket = null; BufferedReader in = null; PrintWriter out = null; try { socket = new Socket(ADDRESS, PORT); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); out.println("Client request"); String response = in.readLine(); System.out.println("Client:" + response); } catch (Exception e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (Exception e1) { e1.printStackTrace(); } } if (out != null) { try { out.close(); } catch (Exception e2) { e2.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (Exception e3) { e3.printStackTrace(); } } socket = null; } } }
程序输出:
Server端:
Server Start......
Server:Client request
Client端:
Client:Server Response