当前位置:网站首页>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
边栏推荐
- 浏览器cookie转selenium cookie登录
- 【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(二)
- 数据库系列:MySQL索引优化与性能提升总结(综合版)
- Topic38——56. 合并区间
- nmcli team bridge 基本配置
- Industry insight - how should brand e-commerce reshape growth under the new retail format?
- Dm8: Dameng database - lock timeout
- 微服务之配置管理中心
- Building crud applications in golang
- Detailed configuration of log4j
猜你喜欢

【粉丝福利】今天给大家介绍一个白捡钱的方法-可转债,本人亲自验证,每年每人能获利1500元

解开C语言的秘密《关键字》(第六期)

树莓派 3b+ 学习

script defer async模式

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

【医学分割】unet3+

全球最快下载工具 XDM

The world's fastest download tool XDM

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

Uni app develops wechat applet to dynamically render pages and dynamically change the order of page component modules
随机推荐
【粉丝福利】今天给大家介绍一个白捡钱的方法-可转债,本人亲自验证,每年每人能获利1500元
Uni app sends request instructions using the escook / request miniprogram plug-in
An interesting experiment of netmask
Browser cookie to selenium cookie login
大小端字节序
消息隊列的使用
Uniapp drop-down layer selection box effect demo (sorting)
AI for Science: scientific research paradigm, open source platform and industrial form
SSH workflow and principle
树莓派 3b+ 学习
Dm8: Dameng database - lock timeout
私藏干货分享:关于企业架构中如何进行平台化
Topic38——56. Consolidation interval
mysql学习1:安装mysql
Uni app develops wechat applet to dynamically render pages and dynamically change the order of page component modules
数据库的复习总结
Topic38——56. 合并区间
ViewPager2使用记录
MIT6.031 软件构造 Reading7阅读笔记Designing Specifications(设计规范)
消息队列的使用