当前位置:网站首页>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
边栏推荐
- 如何下载带有超链接的图片
- 本地可视化工具连接阿里云centOS服务器的redis
- 自学ADT和OOP
- Thymeleaf的相关知识
- application. Configuration information of properties
- [high frequency interview questions] difficulty 1.5/5, LCS template questions
- Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing
- 微服务之配置管理中心
- Getting started with go web programming: validators
- 【粉丝福利】今天给大家介绍一个白捡钱的方法-可转债,本人亲自验证,每年每人能获利1500元
猜你喜欢

Word text box page feed

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

秒云荣获《2022爱分析 · IT运维厂商全景报告》智能运维AIOps市场代表厂商

微服务之配置管理中心

ssh工作流程及原理

数据库系列:MySQL索引优化与性能提升总结(综合版)
![Dynamic programming [III] (interval DP) stone merging](/img/a4/82c4d63b8df5d092a96b80dd497147.jpg)
Dynamic programming [III] (interval DP) stone merging

浏览器cookie转selenium cookie登录

Snipaste, the world's strongest screenshot software

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
随机推荐
行业洞察 | 新零售业态下,品牌电商应如何重塑增长?
build.gradle 配置
自定义多线程基类threading.Event
Minimum editing distance (linear DP writing method)
PyQt,PySide-槽函数被执行了两次
PyCharm汉化
Unzip log. GZ file
微服务之配置管理中心
ViewPager2使用记录
秒云荣获《2022爱分析 · IT运维厂商全景报告》智能运维AIOps市场代表厂商
mysql学习1:安装mysql
The browser enters the URL address, and what happens to the page rendering
Nmcli team bridge basic configuration
AI for Science:科研范式、开源平台和产业形态
号称史上最难618,淘宝数据盘点你做对了吗?
关闭windows defender安全中心的方法
全球最强截图软件 Snipaste
uni-app 使用escook/request-miniprogram插件发请求说明
数据库系列:MySQL索引优化与性能提升总结(综合版)
uniapp下拉弹层选择框效果demo(整理)