当前位置:网站首页>Detailed explanation of socket programming send() and recv() functions
Detailed explanation of socket programming send() and recv() functions
2022-04-23 03:16:00 【Tor_ pedo】
First send The function prototype
ssize_t send (int fd, const void *buf, size_t n, int flags);
ssize_t recv (int fd, void *buf, size_t n, int flags)
fd Indicates the socket to send to ,buf Pointer header indicating the data to be sent ,n Indicates that you want to send ( receive ) The length of ,flag Yes, the logo is generally set to 0.
1:send() recv() Behavioral overview and memset() Importance
send() Your behavior is from buf Place selection n A byte of data is sent out ,recv() Then read n Byte to buf in . Here we need to pay attention to , They are wrong buf For any detection of the value in , Therefore, at the receiving end, it is best to first buffer Set as 0, And set it to be greater than that of each reception buffer The length of
Take a chestnut :
// The sender
char buffer[10] = "1234567";
send(fd,buffer,3,0);
// The receiving party
char buffer[10]=“abcdef”;
recv(fd,buffer,3,0);
After the acceptance is completed buffer Change into "123def", There's no way to distinguish what's received ,
Standard practice :
// The sender
char buffer[10] = "1234567";
send(fd,buffer,3,0);
// The receiving party
char buffer[10]=“abcdef”;
memset(buffer,0,sizeof(buffer));// Let's leave everything blank
recv(fd,buffer,3,0);
2:send() function
about send function , The return value is equal to 0 Indicates that the connection is broken , Less than 0 Indicates an error , Greater than 0 Indicates the number of bytes actually sent successfully . That is, it is possible that the actual number of bytes sent is less than that in the function n( This is a send The function is over , Start executing the next line of code ), Make sure that if n All bytes sent out , Need to write while loop Judge the cumulative number of bytes sent each time ( Generally, all can be sent successfully ).
The return value of blocking mode and non blocking mode has the same meaning .
Blocking mode : If the length of the data to be sent is less than the size of the remaining buffer , Send it directly ; If the length of the data to be sent is greater than the remaining buffer , The block , Wait until the buffer is sent, and the data space becomes larger than the data to be sent several times before , Put it in the buffer , Returns the number of bytes successfully sent ( Maybe not all of them were sent successfully ),0 Indicates that the connection is broken , Less than 0 Indicates an error .
Non-blocking mode : If the length of the data to be sent is less than the size of the remaining buffer , Then send and return success data directly ; If the length of the data to be sent is greater than the remaining buffer, an error is returned ( Less than 0),0 Indicates that the connection is broken , Less than 0 Indicates an error .
In other words, blocking and non blocking are just different modes , The meaning they return is the same .
3 recv() function
recv() Function and send() The return value of the function has the same meaning .
recv The function will be more complicated in practice ,send The function only needs to determine the size to send , Then send it ( Generally, the buffer is large enough , And it can be sent successfully at one time ). But when recv When a function reads from a buffer, it's a little complicated .
In blocking mode : If the buffer is empty , The block , If n Less than the data length of the buffer , Then read from the buffer n Bytes , The rest remains in the buffer , return n; If n If it is equal to the length of the buffer, all are read , Buffer is empty , return n, If n Greater than buffer length , Then read it all , Buffer is empty , Returns the actual read length .
In non-blocking mode : If the buffer is empty , Returns an error , The rest are the same as the blocking mode .
版权声明
本文为[Tor_ pedo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230315331299.html
边栏推荐
- Why is bi so important to enterprises?
- JSON related
- 编码电机PID调试(速度环|位置环|跟随)
- OLED多级菜单记录
- Use of slice grammar sugar in C #
- Impact of AOT and single file release on program performance
- It can receive multiple data type parameters - variable parameters
- IDEA查看历史记录【文件历史和项目历史】
- 2022A特种设备相关管理(电梯)上岗证题库及模拟考试
- A set of C interview questions about memory alignment. Many people make mistakes!
猜你喜欢
Xutils3 corrected a bug I reported. Happy
General test technology [II] test method
Chapter 8 of C language programming (fifth edition of Tan Haoqiang) is good at using pointer exercises to analyze and answer
超好用的Excel异步导出功能
編碼電機PID調試(速度環|比特置環|跟隨)
ASP. Net 6 middleware series - conditional Middleware
2022G2电站锅炉司炉考试题库及在线模拟考试
《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
ASP. Net and ASP NETCORE multi environment configuration comparison
[untitled]
随机推荐
再战leetcode (290.单词规律)
OLED multi-level menu record
Mysql database, inconsistent index character set, slow SQL query, interface timeout
be based on. NETCORE development blog project starblog - (1) why do you need to write your own blog?
ASP. Net and ASP NETCORE multi environment configuration comparison
How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing
[Mysql] LEFT函數 | RIGHT函數
Use of ADB command [1]
MySQL之explain关键字详解
Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
Use of slice grammar sugar in C #
The backtracking of stack is used to solve the problem of "the longest absolute path of file"
[mock data] fastmock dynamically returns the mock content according to the incoming parameters
Top 9 task management system in 2022
Two methods are used to solve the "maximum palindrome product" problem
场景题:A系统如何使用B系统的页面
[authentication / authorization] customize an authentication handler
2022a special equipment related management (elevator) work license question bank and simulation examination
Impact of AOT and single file release on program performance
Establishing and traversing binary tree