当前位置:网站首页>网络通信编程基础,BIO,NIO
网络通信编程基础,BIO,NIO
2022-07-29 20:53:00 【yfyh2021】
编程中的Socket是什么?
Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口,其实就是一个门面模式。
BIO的阻塞提现在两个方面
若一个服务器启动就绪,那么服务器的主线程就一直等待着客户端连接,这个等待过程中主线程就在阻塞。
public class ServerSingle {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket();
serverSocket.bind(new InetSocketAddress(8888));
System.out.println(“Start Server …”);
int connectCount = 0;
try {
while (true){
Socket socket = serverSocket.accept();System.out.println("accept client socket ....total =" + ( ++connectCount)); ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream()); ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream()); String userName = inputStream.readUTF(); System.out.println("Accept client message:"+userName); outputStream.writeUTF("Hello,"+userName); outputStream.flush(); } }catch (Exception e){ e.printStackTrace(); }finally { serverSocket.close(); }}
}
可以看到主线程启动,线程阻塞在了accept()监听方法,后面的没有打印
在连接建立之后,在读到socket信息之前,线程也是一直在等待的,阻塞在那里。
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = null;
ObjectOutputStream output = null;
ObjectInputStream input = null;
InetSocketAddress addr
= new InetSocketAddress(“127.0.0.1”,8888);try { socket = new Socket(); socket.connect(addr); System.out.println("Connect Server success!!"); output = new ObjectOutputStream(socket.getOutputStream()); input = new ObjectInputStream(socket.getInputStream()); System.out.println("Ready send message....."); output.writeUTF("yang"); output.flush(); System.out.println(input.readUTF()); } catch (IOException e) { e.printStackTrace(); }finally { if (socket!=null) socket.close(); if (output!=null) output.close(); if (input!=null) input.close(); }}
}
我们在client中打上断点,debug启动。可以看到client连接成功,server端的accept也接收到了,但是主线程又阻塞在了等待socket的输出当中
NIO和BIO的主要区别
- NIO和IO的主要区别是IO是面向流的,而NIO是面向缓冲区的。
- 阻塞与非阻塞
NIO三大核心组件
- Selector。记录发生的事件,以及服务端和客户端关注的事件。
- channel。本质上就是socket的包装类。分为serverSocketChannel和socketChannel,serverSocketChannel只关心accept的事件,也只有服务端才会关心;而socketChannel就是普通的socket关心读写事件。客户端的socketChannel关心connect事件。
- buffer。缓冲区,我们上面说到NIO是面向缓冲区的,就是这个buffer,就是因为buffer的存在,使得线程不需要等待。
重要概念:SelectionKey
SelectionKey是一个抽象类,表示selectableChannel在Selector中注册的标识,其中有四种状态分别是OP_READ(读事件),OP_WRITE(写事件),OP_CONNECT(发起连接事件),OP_ACCEPT(接收连接事件)
直接内存比堆内存快在哪
jvm在向socket缓冲区发送信息时,会在堆外重新开辟一块内存,将要发送的信息拷贝进去。这样做的原因是防止在gc的时候,改变堆内信息的位置。
堆外内存的优点和缺点
堆外内存相比于堆内内存有几个优势:
1 减少了垃圾回收的工作,因为垃圾回收会暂停其他的工作(可能使用多线程或者时间片的方式,根本感觉不到)
2 加快了复制的速度。因为堆内在flush到远程时,会先复制到直接内存(非堆内存),然后在发送;而堆外内存相当于省略掉了这个工作。
而福之祸所依,自然也有不好的一面:
1 堆外内存难以控制,如果内存泄漏,那么很难排查
2 堆外内存相对来说,不适合存储很复杂的对象。一般简单的对象或者扁平化的比较适合。
边栏推荐
- [Database] mysql date format conversion
- 赶紧进来!!!带你认识C语言基本数据类型
- 写出优雅的Kotlin代码:聊聊我认为的 “Kotlinic“
- LeetCode 0144. 二叉树的前序遍历:二叉树必会题
- PyQt5学习一(环境搭建)
- Second Best PyTorch Beginner Course; Thesis Writing Guide; Using µGo to Develop a Mini Compiler; Super Efficient Use of Transformer's Extension Library; Frontier Papers | ShowMeAI News Daily
- Come in now!!!Take you to know the basic data types of C language
- LeetCode 593 Valid Squares [Math] HERODING's Road to LeetCode
- 南信大提出TIPCB,一个简单但有效的用于基于文本的人员搜索的基于部分的卷积baseline
- 指定宽度截取字符串
猜你喜欢

OneNote 教程,如何在 OneNote 中做笔记?

1. Promise usage in JS, 2. The concept and usage of closures, 3. The difference between the four methods and areas of object creation, 4. How to declare a class

The younger brother asked: Is the work of a programmer a day’s work of code?

《张卫国的夏天》欢乐来袭,黄磊、刘奕君携手演绎“冤种”兄弟

回归——分层回归

First thoughts on the first attempt to avoid killing without a file (Part 1)

阿里 P8 爆出的这份大厂面试指南,看完工资暴涨 30k!

240. 搜索二维矩阵 II

Cobaltstrike和BurpSuite桌面快捷配置

怎么实现您的个人知识库?
随机推荐
7 行代码搞崩溃 B 站,原因令人唏嘘!
第3章业务功能开发(线索关联市场活动,动态搜索)
初识网络的简单概念
一 JS中Promise用法、二闭包的概念与用法、三对象创建的四种方式与区区别、四 如何声明一个类
【593. 有效的正方形】
太卷了,企业级的智慧物业系统,也完全开源....
The younger brother asked: Is the work of a programmer a day’s work of code?
从实例学Kettle(一):获取股票行情数据
惠普服务器硬盘指示灯不亮或显示蓝色
leetcode-593:有效的正方形
LeetCode 0144. 二叉树的前序遍历:二叉树必会题
【无标题】
OneNote tutorial, how to take notes in OneNote?
[ACTF2020 新生赛]Exec 1
阿里 P8 爆出的这份大厂面试指南,看完工资暴涨 30k!
一线技术人应该关注的四种思维能力
Where is Naive Bayes "naive"?
.NET 6.0中使用Identity框架实现JWT身份认证与授权
Writing Elegant Kotlin Code: Talk About What I Think "Kotlinic"
根据昵称首字母生成头像