网络编程的基本模型是Client/Server模型,也就是两个进程之间进行相互通信,其中服务端提供位置信息(
绑定ip地址和监听端口),客户端通过连接操作向服务端监听的地址发送连接请求,通过三次握手建立连接,
如果连接成功,双方就可以通过socket进行通信。
在基于传统的同步阻塞模型开发中,ServerSocket负责绑定IP地址,启动监听端口:Socket负责发起连接请求
操作。操作连接成功后,双方通过输入和输出流进行同步阻塞通信。
下面是经典的时间服务器代码,分析工作过程:
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根据传入参数设置监听的端口,如果没有入参,使用默认8080端口,通过构造函数创建ServerSocket
,如果端口合法且没有被占用,服务端监听成功。程序中通过一个循环来监听客户端的接入,如果没有客户端的
接入,则线程阻塞在ServerSocket的accept操作上,启动TimeServer,等待客户端的接入
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; } } } }
客户端代码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的弊端:
每当一个新的客户端接入请求时,服务器必须创建一个新的线程处理新接入的链路,一个线程只能处理一个
客户端的连接。在高性能服务器的应用领域,往往需要成千上万个客户端的并发连接,这种模型无法满足高性能
、高并发接入的场景!!!!
BIO介绍完毕~~~~~~~
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-25 23:11:47