网络编程的基本模型是Client/Server模型。也就是两个进程之间进行相互通信,当中服务端提供位置信息(
绑定ip地址和监听port),client通过连接操作向服务端监听的地址发送连接请求,通过三次握手建立连接。
假设连接成功。两方就能够通过socket进行通信。
在基于传统的同步堵塞模型开发中。ServerSocket负责绑定IP地址,启动监听port:Socket负责发起连接请求
操作。操作连接成功后,两方通过输入和输出流进行同步堵塞通信。
以下是经典的时间server代码,分析工作过程:
TimeServer代码:
package com.panther.dong.netty.bio.synchronousblockio; import java.net.ServerSocket; import java.net.Socket; /** * server thread(corresponding to all client thread) * Created by panther on 15-8-11. */ public class TimeServer { public static void main(String[] args) { int port = 8080; if (args != null && args.length > 0) { port = Integer.valueOf(args[0]); } ServerSocket server = null; try { server = new ServerSocket(port); System.out.println("the time server is start in port: " + port); Socket socket = null; while (true) { socket = server.accept(); new Thread(new TimeServerHandler(socket)).start(); } } catch (Exception e) { e.printStackTrace(); } finally { if (server != null) { System.out.println("the time server close"); try { server.close(); } catch (Exception e) { } server = null; } } } }
TimerServer依据传入參数设置监听的port。假设没有入參,使用默认8080port。通过构造函数创建ServerSocket
。假设port合法且没有被占用。服务端监听成功。
程序中通过一个循环来监听client的接入,假设没有client的
接入,则线程堵塞在ServerSocket的accept操作上。启动TimeServer,等待client的接入
TimeServerHandler的代码:
package com.panther.dong.netty.bio.synchronousblockio; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.util.Date; /** * listener client socket * Created by panther on 15-8-11. */ public class TimeServerHandler implements Runnable { private Socket socket; public TimeServerHandler(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 current = null; String body = null; while (true) { body = in.readLine(); if (body == null) { break; } System.out.println("The time server receive order : " + body); current = "QUERY TIME ORDER".equalsIgnoreCase(body) ? new Date(System.currentTimeMillis()).toString() : "BAD ORDER"; out.println(current); } } catch (Exception e) { if (in != null) { try { in.close(); } catch (IOException e1) { e1.printStackTrace(); } } if (out != null) { out.close(); out = null; } if (this.socket != null) { try { this.socket.close(); } catch (IOException e1) { e1.printStackTrace(); } this.socket = null; } } } }
client代码TimeClient:
package com.panther.dong.netty.bio.synchronousblockio; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; /** * client thread(one thread) * Created by panther on 15-8-13. */ public class TimeClient { public static void main(String[] args) { int port = 8080; if (args != null && args.length > 0) { try { port = Integer.valueOf(args[0]); } catch (Exception e) { } } Socket socket = null; BufferedReader in = null; PrintWriter out = null; try { socket = new Socket("127.0.0.1", port); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); out.println("QUERY TIME ORDER"); System.out.println("Send order 2 server succeed."); String resp = in.readLine(); System.out.println("Now is : " + resp); } catch (IOException e) { } finally { if (out != null) { out.close(); out = null; } if (in != null) { try { in.close(); } catch (IOException e) { } in = null; } if (socket != null) { try { socket.close(); } catch (IOException e) { } socket = null; } } } }
执行结果:
先执行TimeServer得到结果:
在执行TimeClient。得到例如以下结果:
由上面的图可知道。TimeServer和TimeClient建立连接!
!
!!
BIO的弊端:
每当一个新的client接入请求时,server必须创建一个新的线程处理新接入的链路。一个线程仅仅能处理一个
client的连接。在高性能server的应用领域,往往须要成千上万个client的并发连接。这样的模型无法满足高性能
、高并发接入的场景。!
。。
BIO介绍完成~~~~~~~