当前位置:网站首页>Socket blocking and non blocking modes
Socket blocking and non blocking modes
2022-06-27 12:43:00 【xiongsiyu979】
socket Blocking and non blocking modes
This article discusses Linux Under the socket Socket
One 、 Sync 、 Asynchronous blocking and non blocking
- Sync : Ask and wait IO How the operation is completed
- asynchronous : After actively requesting data , You can handle other tasks , Then wait IO Notice of completion of operation
- Blocking : The thread continues to wait for the data in the resource to be ready , Until a response is returned
- Non blocking : The thread returns the result directly , Does not wait for the resource to prepare the data before responding to the result
summary :
- Synchronous and asynchronous refer to IO operation , Synchronization is the thread waiting IO Completion , Induction is IO Upon completion, the thread will receive a notification
- Blocking and non blocking are threads , Blocking may occur at IO period , It can also happen in IO Before
for example , For a simple server program (socket、bind、listen、accept), We usually say that the program will block in accept() It's about , and accept() The operation of the function is synchronous
Two 、 What is? socket Blocking / Non-blocking mode ?
1) Establishing a connection connect
Blocking mode ,connect First send SYN Request to server , When the client receives a message from the server SYN When confirming , be connect return , Otherwise, it will be blocked all the time .
Non blocking mode ,connect Will enable TCP Three handshakes of the agreement , however connect The function does not wait for the connection to be established before returning , It's about going back to , The error code returned is EINPROGRESS, Indicates that a process is in progress .
2) Receiving connection accept
- Call... In blocking mode accept() function , When there is no new connection , The process goes to sleep , Until a connection is available , To return to .
- Call... In non blocking mode accept() The function immediately returns , There are connections that return client socket descriptors . When there is no new connection , Will return EWOULDBLOCK Error code , It means that it should have been blocked .
3) Read and write operations read/write
- When the read / write buffer is filled , In blocking mode, the program will block in read/write, Instead of blocking mode read/write The error code will be returned directly
3、 ... and 、 How to set up socket Blocking / Non-blocking mode ?
- socket() Function created socket The default is blocking Of
- socket() The function prototype :
int socket(int domain, int type, int protocol);
/*1.domain: Protocol domain , Also known as protocol family , It determines socket Address type of * 2.type: Appoint socket type , It is associated with protocol Corresponding * 3.protocol: Specify transport protocol , Such as IPPROTO_TCP The specified transport protocol is TCP,IPPTOTO_UDP The specified transport protocol is UDP, When protocol by 0 when , Will automatically select type Default protocol corresponding to type */
- take socket Setting to non blocking mode has Two kinds of Way :
- stay establish socket when , Specify the created socket Non blocking (type Parameter SOCK_NONBLOCK sign )
int sockfd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP);
- Use fcntl() and ioctl() Function settings socket Is non-blocking mode
fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL, 0) | O_NONBLOCK);
ioctl(sockfd, FIONBIO, 1); //1: Non blocking 0: Blocking
边栏推荐
- Thymeleaf的相关知识
- How to open an account for CSI 500 stock index futures, what are the regular domestic stock index futures platforms, and where is the safest place to open an account?
- 对象序列化
- Talk about go language and cloud native technology
- Configuration management center of microservices
- application. Configuration information of properties
- Tidb 6.0: making Tso more efficient tidb Book rush
- How to find the movie and TV clips with the same lines? These 8 movies search for artifact, and find the corresponding segment in one line
- nmcli team bridge 基本配置
- application.properties 的配置信息
猜你喜欢
Interview shock 60: what will cause MySQL index invalidation?
ssh工作流程及原理
Configuration management center of microservices
JMETER连接DM8
gcc编译动态库和静态库
Local visualization tool connects to redis of Alibaba cloud CentOS server
Topic37——64. Minimum path sum
uniapp下拉弹层选择框效果demo(整理)
An interesting experiment of netmask
Private dry goods sharing: how to implement platform in Enterprise Architecture
随机推荐
阿里一个面试题:使用两个线程,交替输出字母和数字
如何下载带有超链接的图片
LeetCode_快速幂_递归_中等_50.Pow(x, n)
Object serialization
私藏干货分享:关于企业架构中如何进行平台化
Interview shock 60: what will cause MySQL index invalidation?
Time management understood after being urged to work at home
让学指针变得更简单(二)
mybaitis生成器详解
word文本框换页
What's the matter with Amazon's evaluation dropping and failing to stay? How to deal with it?
ssh工作流程及原理
消息隊列的使用
【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(三)
. Net6 access skywalking link tracking complete process
【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(二)
[on Nacos] get started quickly
全球最强截图软件 Snipaste
Mybaitis generator details
微服务之配置管理中心