当前位置:网站首页>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
边栏推荐
- Improvement of ref and struct in C 11
- [authentication / authorization] customize an authentication handler
- Flink real-time data warehouse project - Design and implementation of DWS layer
- Maui initial experience: Cool
- ASP. Net 6 middleware series - execution sequence
- Using stack to solve the problem of "mini parser"
- C read / write binary file
- PID debugging of coding motor (speed loop | position loop | follow)
- Comprehensive calculation of employee information
- 数据库表中不建索引,在插入数据时,通过sql语句防止重复添加(转载)
猜你喜欢

Source generator actual combat
![Use of ADB command [1]](/img/e6/fb8fd6956c226f75b831f8eb46277f.png)
Use of ADB command [1]

2022 P cylinder filling training test questions and simulation test

Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800

2022 Shandong Province safety officer C certificate work certificate question bank and online simulation examination

Drawing polygons with < polygon / > circular array in SVG tag

二进制文件版本控制工具选择难?看完这篇你会找到答案

ASP. Net 6 middleware series - execution sequence

Is it difficult to choose binary version control tools? After reading this article, you will find the answer

可以接收多種數據類型參數——可變參數
随机推荐
Iotos IOT middle platform is connected to the access control system of isecure center
[MySQL] left function | right function
Docker拉取mysql并连接
How does Microsoft solve the problem of multiple programs on PC side -- internal implementation
Blazor University (12) - component lifecycle
The backtracking of stack is used to solve the problem of "the longest absolute path of file"
Course design of Database Principle -- material distribution management system
2022 Shandong Province safety officer C certificate work certificate question bank and online simulation examination
How does Microsoft solve the problem of multiple PC programs
Top 9 task management system in 2022
软件测试相关知识~
Using positive and negative traversal to solve the problem of "the shortest distance of characters"
[untitled]
MySQL之explain关键字详解
Fiddler use
编码电机PID调试(速度环|位置环|跟随)
12.<tag-链表和常考点综合>-lt.234-回文链表
Mysql database
Use split to solve the "most common words" problem
JS implementation of new