概念
客户端:
1 package bhz.bio2; 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 import java.net.UnknownHostException; 9 10 public class Client { 11 12 final static String ADDRESS = "127.0.0.1"; 13 final static int PORT =8765; 14 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 out.println("Client request"); 25 26 String response = in.readLine(); 27 System.out.println("Client:" + response); 28 29 30 } catch (Exception e) { 31 // TODO Auto-generated catch block 32 e.printStackTrace(); 33 } finally { 34 if(in != null){ 35 try { 36 in.close(); 37 } catch (Exception e1) { 38 e1.printStackTrace(); 39 } 40 } 41 if(out != null){ 42 try { 43 out.close(); 44 } catch (Exception e2) { 45 e2.printStackTrace(); 46 } 47 } 48 if(socket != null){ 49 try { 50 socket.close(); 51 } catch (Exception e3) { 52 e3.printStackTrace(); 53 } 54 } 55 socket = null; 56 } 57 58 59 60 } 61 62 }
package bhz.bio2; import java.io.BufferedReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; 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 (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(server != null){ try { server.close(); } catch (Exception e3) { e3.printStackTrace(); } } server = null; } } }
线程池:
package bhz.bio2; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; 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){ this.executor.execute(task); } }
package bhz.bio2; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; 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 { 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) break; System.out.println("Server:" + body); out.println("Server 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; } } }