当前位置:网站首页>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
边栏推荐
- Blazor University (12) - component lifecycle
- Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis
- 《C语言程序设计》(谭浩强第五版) 第9章 用户自己建立数据类型 习题解析与答案
- Establishing and traversing binary tree
- The most detailed in the whole network, software testing measurement, how to optimize software testing cost and improve efficiency --- hot
- poi根据数据创建导出excel
- Yes Redis using distributed cache in NE6 webapi
- 2022g2 boiler stoker examination question bank and online simulation examination
- 2022G2电站锅炉司炉考试题库及在线模拟考试
- After the mobile phone is connected to the computer, how can QT's QDIR read the mobile phone file path
猜你喜欢

Yes Redis using distributed cache in NE6 webapi

由于3²+4²=5²,所以称‘3,4,5‘为勾股数,求n(包括n)以内所有勾股数数组。

A comprehensive understanding of static code analysis

Tencent video price rise: earn more than 7.4 billion a year! Pay attention to me to receive Tencent VIP members, and the weekly card is as low as 7 yuan
![[mock data] fastmock dynamically returns the mock content according to the incoming parameters](/img/1e/c4aad49d16fb21a19865ba75128e43.png)
[mock data] fastmock dynamically returns the mock content according to the incoming parameters

2022G2电站锅炉司炉考试题库及在线模拟考试

可以接收多種數據類型參數——可變參數

Student achievement management

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

After the mobile phone is connected to the computer, how can QT's QDIR read the mobile phone file path
随机推荐
ASP. Net 6 middleware series - conditional Middleware
再战leetcode (290.单词规律)
2022 Shandong Province safety officer C certificate work certificate question bank and online simulation examination
Drawing polygons with < polygon / > circular array in SVG tag
[new version release] componentone added Net 6 and blazor platform control support
2022年P气瓶充装培训试题及模拟考试
搭建XAMPP时mysql端口被占用
全新的ORM框架——BeetlSQL介绍
2022年做跨境电商五大技巧小分享
How does Microsoft solve the problem of multiple programs on PC side -- internal implementation
Charles uses three ways to modify requests and responses
建立与遍历二叉树
编码电机PID调试(速度环|位置环|跟随)
Miniapi of. Net7 (special section): NET7 Preview3
[MySQL] left function | right function
Ide-idea-problem
Tencent video VIP member, weekly card special price of 9 yuan! Tencent official direct charging, members take effect immediately!
Find the number of leaf nodes of binary tree
ASP. Net and ASP NETCORE multi environment configuration comparison
在.NE6 WebApi中使用分布式缓存Redis