netty学习(二)--传统的bio编程
来源:互联网 发布:怎么用语法创建数据库 编辑:程序博客网 时间:2024/06/05 00:08
网络编程的基本模型是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介绍完毕~~~~~~~
0 0
- netty学习(二)--传统的bio编程
- 《netty权威指南》2.1传统的BIO编程
- 1、传统BIO编程示例及缺点分析(netty-学习笔记)
- NIO入门之传统的BIO编程
- 传统BIO编程
- Java Netty 学习笔记(二)使用Netty编程
- Netty框架(一)————BIO编程
- java 网络编程(二)TCPIP+BIO
- netty系列3-Java BIO编程
- Netty学习(二)-Helloworld Netty
- Netty学习之二--Java socket编程(UDP实例)
- Netty学习之BIO(阻塞IO)
- 网络编程复习(二):JDK1.5之前的伪异步BIO编程
- BIO,NIO,AIO及netty的介绍
- 【网络编程】(四)BIO传统版、多线程版、线程池版对比
- 传统Socket编程的回顾(二)
- 二、java网络io编程(BIO、NIO)
- Java 网络IO编程总结二(BIO、NIO、AIO)
- UVA 11426 GCD - Extreme (II) (数论|欧拉函数)
- Can't find keyplane that supports type 4 for keyboard iPhone-PortraitChoco-NumberPad; using 27057872
- UNIX环境高级编程之第5章:标准I/O库
- 关于对象深拷贝的一个方法(非Cloneable方式)
- ant安装配置,一个ant的简单实例
- netty学习(二)--传统的bio编程
- java和mysql进行连接
- 多态
- Foundation框架—结构体
- 水仙花数
- Centos 安装后无法使用ipv4地址
- RT-Thread finsh源码分析: finsh_node.h
- 集合的知识点
- bootstrap的多选下拉框的使用