当前位置:网站首页>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
边栏推荐
- 【粉丝福利】今天给大家介绍一个白捡钱的方法-可转债,本人亲自验证,每年每人能获利1500元
- 秒云荣获《2022爱分析 · IT运维厂商全景报告》智能运维AIOps市场代表厂商
- log4j的详情配置
- 剑指 Offer 04. 二维数组中的查找
- ACL 2022 | 中科院提出TAMT:TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络
- Interview shock 60: what will cause MySQL index invalidation?
- 今晚战码先锋润和赛道第2期直播丨如何参与OpenHarmony代码贡献
- 如何下载带有超链接的图片
- 浏览器cookie转selenium cookie登录
- esp32s3 IPERF例程测试 esp32s3吞吐量测试
猜你喜欢

The world's fastest download tool XDM

亚马逊测评掉评、留不上评是怎么回事呢?要如何应对?

让学指针变得更简单(一)

Neo4j: basic introduction (I) installation and use

How histrix works

MySQL high level statements (I)

Database Series: MySQL index optimization and performance improvement summary (comprehensive version)

Three traversal methods of binary tree

本地可视化工具连接阿里云centOS服务器的redis

Operators are also important if you want to learn the C language well
随机推荐
It is so simple to remove the payment restrictions on VIP, YuQue and Zhihu in Baidu Library
关于枚举类的两种用法
Secyun won the "2022 AI analysis · it operation and maintenance vendor panorama report" as the representative vendor of intelligent operation and maintenance aiops Market
Tidb 6.0: making Tso more efficient tidb Book rush
Unlock the secret of C language key words (issue 6)
PyQt,PySide-槽函数被执行了两次
Time management understood after being urged to work at home
剑指 Offer 04. 二维数组中的查找
How to close windows defender Security Center
Three traversal methods of binary tree
带你认识图数据库性能和场景测试利器LDBC SNB
yml的配置
【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(二)
浏览器cookie转selenium cookie登录
让学指针变得更简单(二)
本地可视化工具连接阿里云centOS服务器的redis
私藏干货分享:关于企业架构中如何进行平台化
Configuration management center of microservices
How to download pictures with hyperlinks
秒云荣获《2022爱分析 · IT运维厂商全景报告》智能运维AIOps市场代表厂商