本文概览:介绍了基于JAVA bio和伪bio实现的sockt通信。一次客户端和服务端通信主要包括 serversocket.accept()、socket.connect、read data 、wrire data、sokcet.close() 五个步骤。
TCP知识
1 单服务端和单客户端
1、socket server的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public class BioServer { public void start() { BufferedReader in = null; PrintWriter out = null; try { ServerSocket serverSocket = new ServerSocket(8090); while (true) { Socket socket = serverSocket.accept(); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); String line; // 1.接受数据 System.out.println("Server接受数据为:" + in.readLine()); // 2.发送给客户端数据 out.println("....response data...."); } } catch (Exception e) { System.out.printf("启动失败..."); } } } |
2、Socket客户端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class BioClinet { public void start(){ BufferedReader in = null; PrintWriter out = null; Socket client = new Socket(); try { client.connect(new InetSocketAddress("127.0.0.1", 8090)); in = new BufferedReader(new InputStreamReader(client.getInputStream())); out = new PrintWriter(client.getOutputStream(),true); // 1.发送数据 out.println("...request data..."); // 2.接受数据 System.out.println("Client接受数据为为:" + in.readLine()); }catch (Exception e){ System.out.printf("客户端建立连接失败...."); } } } |
3、测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class Test { public static void main(String[] args) { // 1.启动服务器线程 final BioServer server = new BioServer(); Thread severThread = new Thread(new Runnable() { public void run() { server.start(); } }) ; severThread.start(); // 2.启动客户端线程 final BioClinet clinet = new BioClinet(); Thread clientThread = new Thread(new Runnable() { public void run() { clinet.start(); } }); clientThread.start(); } } |
4、执行结果为:
1 2 3 |
Server接受数据为:...request data... Client接受数据为为:....response data.... Client接受数据为为:....response data.... |
2 单服务端和多客户端
为了一个socket服务端同时处理多个客户端请求,可以每次在接受到一个客户端请求,创建了socket之后,把这个socket交给一个线程去维护。
具体实现如下:
1、修改client Server代码为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
public class MultiThreadBioServer { // 定义线程池 private ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(2, 10, 1, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()); /** * 启动服务 */ public void start() { try { ServerSocket serverSocket = new ServerSocket(8090); while (true) { Socket socket = serverSocket.accept(); poolExecutor.execute(new Task(socket)); } } catch (Exception e) { System.out.printf("启动失败..."); } } /** * 自定义task */ class Task implements Runnable { private Socket socket; public Task(Socket socket) { this.socket = socket; } public void run() { try { BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); String line; // 接受数据 System.out.println("Server接受数据为:" + in.readLine()); // 发送给客户端数据 out.println("....response data...."); }catch (Exception e){ } } } } |
2、测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
public class MultiThreadTest { public static void main(String[] args) { // 1.启动服务器线程 final MultiThreadBioServer server = new MultiThreadBioServer(); Thread severThread = new Thread(new Runnable() { public void run() { server.start(); } }); severThread.start(); // 2.启动两个客户端线程 final BioClinet clinet = new BioClinet(); // 2.1 客户端线程1 Thread clientThread1 = new Thread(new Runnable() { public void run() { clinet.start(); } }); clientThread1.start(); // 2.1 客户端线程2 Thread clientThread2 = new Thread(new Runnable() { public void run() { clinet.start(); } }); clientThread2.start(); } } |
执行结果为
1 2 3 4 |
Server接受数据为:...request data... Server接受数据为:...request data... Client接受数据为为:....response data.... Client接受数据为为:....response data.... |