首页 技术 正文
技术 2022年11月18日
0 收藏 421 点赞 4,027 浏览 2545 个字

一、什么是BIO

  BIO是传统的通信技术,在BIO通信模型中,客户端发送请求给服务器,服务器每次都是会单独创建一个线程来监控客户端的请求,会为每个客户端创建一个线程来处理请求。当前服务器处理完成后,通过原来的输出流返回处理结果给到客户端。如图:

Netty学习–第一章 JDK自带的BIO

二、JDK实现BIO的案例

  按照上面的图,我们知道至少需要三给类来完成,一个客户端,一个服务端,一个服务端逻辑处理:

  服务器代码:

  

/**
* 服务端代码
*/
public class BioServer { //端口号
private final static int port = 8080; public static void main(String[] args) {
//创建一个ServerSocket对象,作为服务器
ServerSocket server = null;
try {
server = new ServerSocket(port);
System.out.println("服务器启动,端口号为:" + port);
Socket socket = null;
while (true) {
//阻塞服务器,等待客户端发送数据
socket = server.accept();
//客户端发送了数据,开始处理
new Thread(new BioServerShow(socket)).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (server != null) {
System.out.println("The time server close");
try {
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
server = null;
}
} }}

  服务器逻辑处理类:

  

/**
* 服务器处理数据逻辑类
*/
public class BioServerShow implements Runnable{
private Socket socket; public BioServerShow(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("客户端发生的请求是: " + body);
out.println("你好");
} } 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;
}
}
}
}

  客户端代码:

  

/**
* 客户端代码
*/
public class BioClient {
public static void main(String[] args) {
int port = 8080;
//创建一个socke对象,连接服务器,IP和端口必须一致
Socket socket = null;
//输入流的内容表示服务端带来的消息
BufferedReader in = null;
//输出流的内容表示客户端要发送给服务端的请求
PrintWriter out = null;
try {
socket = new Socket("127.0.0.1", port);
System.out.println("客户端启动");
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
out.println("你好,我是客户端");
String resp = in.readLine();
System.out.println("服务器的消息是: " + resp);
} catch (Exception e) {
} finally {
if (out != null) {
out.close();
out = null;
} if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
in = null;
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
socket = null;
}
}
}}

  测试:

  我们先运行服务端代码:

  Netty学习–第一章 JDK自带的BIO

  然后运行客户端代码:

  Netty学习–第一章 JDK自带的BIO

  接下来我们在看服务端的控制台,发现有客户端的数据打印出来:

  Netty学习–第一章 JDK自带的BIO

三、BIO通信的优点与缺点

  先说优点:

  1.模型简单,这个从上面的那张图就可以明白,BIO的模型是很简单的:客户端发送请求,服务器阻塞分发请求到处理的线程,最后将结果返回给客户端。

  2.编码简单,这个也能从我们的代码中看见,编码逻辑都很简易的。

  再说说缺点:

  由于BIO的模型,我们就能发现这个模型最大的缺点就是性能太低,特别是在请求数过多的高并发下,服务器CPU的上下文切换太耗费资源,就是线程的创建与销毁。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,111
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,922