当前位置:网站首页>socket编程 send()与 recv()函数详解
socket编程 send()与 recv()函数详解
2022-04-23 03:15:00 【Tor_pedo】
首先send函数原型
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表示要发送到的套接字,buf表示要发送的数据的指针头,n表示要发送(接收)的长度,flag是标识一般设为0.
1:send() recv()行为总览以及memset()的重要性
send()的行为就是从buf处挑选n个字节的数据发送出去,recv()则读取n个字节到buf中。这里需要注意,他们是不对buf里的值进行任何检测的,所以在接收端最好先将buffer置为0,并设置得大于每次接收的buffer的长度
举个栗子:
//发送方
char buffer[10] = "1234567";
send(fd,buffer,3,0);
//接收方
char buffer[10]=“abcdef”;
recv(fd,buffer,3,0);
接收方在接受完成后buffer变为了"123def",没法区分哪些是接收的,
标准做法:
//发送方
char buffer[10] = "1234567";
send(fd,buffer,3,0);
//接收方
char buffer[10]=“abcdef”;
memset(buffer,0,sizeof(buffer));//先都置为空
recv(fd,buffer,3,0);
2:send()函数
对于send函数,返回值等于0表示连接断开,小于0表示错误,大于0表示实际发送成功的字节数。也就是说有可能存在发送的实际字节数小于函数中的n(这是send函数已经结束了,开始执行下一行代码),如果要确保n个字节全部发送出去了,需要写while循环 判断每次发送的字节数累加是多少(一般是能全部发送成功的)。
阻塞模式和非阻塞模式返回值表示的意义相同。
阻塞模式:如果要发送的数据长度小于剩余缓冲区的大小,则直接发送;如果要发送的数据长度大于剩余缓冲区,则阻塞,一直等到缓冲区发送完前几次数据空间变到大于要发送的数据,放入缓冲区,返回成功发送的字节数(可能没有全部成功发送),0表示连接断开,小于0表示错误。
非阻塞模式:如果要发送的数据长度小于剩余缓冲区的大小,则直接发送返回成功数据;如果要发送的数据长度大于剩余缓冲区则返回错误(小于0),0表示连接断开,小于0表示错误。
也就是说阻塞和非阻塞只是模式不同,他们的返回值表示的意义是一样的。
3 recv()函数
recv()函数与send()函数返回值的意义相同。
recv函数在实际操作上会比较复杂,send函数只需要确定要发送的大小,然后发送就行(一般缓冲区是够大的,而且一次性能发送成功)。但是当recv函数从缓冲区读时就有点复杂。
在阻塞模式下:如果缓冲区为空,则阻塞,如果n小于缓冲区的数据长度,那就从缓冲区读n个字节,剩余的留在缓冲区,返回n;如果n等于缓冲区长度则全部读完,缓冲区为空,返回n,如果n大于缓冲区长度,则全部读完,缓冲区为空,返回实际读的长度。
在非阻塞模式下:如果缓冲区为空,则返回错误,其余和阻塞模式相同。
版权声明
本文为[Tor_pedo]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_44845956/article/details/124349385
边栏推荐
- Huawei mobile ADB devices connection device is empty
- [MySQL] left function | right function
- Mysql database design specification
- Mise en service PID du moteur de codage (anneau de vitesse | anneau de position | suivant)
- A set of C interview questions about memory alignment. Many people make mistakes!
- It can receive multiple data type parameters - variable parameters
- Iotos IOT middle platform is connected to the access control system of isecure center
- 一文了解全面静态代码分析
- Aspnetcore configuration multi environment log4net configuration file
- Maui initial experience: Cool
猜你喜欢
搭建XAMPP时mysql端口被占用
2022 Shandong Province safety officer C certificate work certificate question bank and online simulation examination
be based on. NETCORE development blog project starblog - (1) why do you need to write your own blog?
Course design of Database Principle -- material distribution management system
Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis
Peut recevoir plusieurs paramètres de type de données - paramètres variables
Source generator actual combat
Ningde's position in the times is not guaranteed?
C read / write binary file
Xamarin effect Chapter 21 expandable floating operation button in GIS
随机推荐
[mock data] fastmock dynamically returns the mock content according to the incoming parameters
Chapter 7 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of modular programming exercises with functions
ASP. Net 6 middleware series - conditional Middleware
Middle and rear binary tree
Huawei mobile ADB devices connection device is empty
Drawing polygons with < polygon / > circular array in SVG tag
为什么BI对企业这么重要?
General test technology [II] test method
超好用的【通用Excel导入功能】
2022G2电站锅炉司炉考试题库及在线模拟考试
Queue storage and circular queue
After the mobile phone is connected to the computer, how can QT's QDIR read the mobile phone file path
Fight leetcode again (290. Word law)
Improvement of ref and struct in C 11
Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis
关于idea调试模式下启动特别慢的优化
[MySQL] left function | right function
中后二叉建树
IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统
ASP. Net and ASP NETCORE multi environment configuration comparison