当前位置:网站首页>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
边栏推荐
- 2022年P气瓶充装培训试题及模拟考试
- 月薪10k-20k都无法回答的事务问题,你会吗?
- 数据库表中不建索引,在插入数据时,通过sql语句防止重复添加(转载)
- Improvement of ref and struct in C 11
- Chapter 7 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of modular programming exercises with functions
- [Mysql] LEFT函数 | RIGHT函数
- oracle 查询外键含有逗号分隔的数据
- The most easy to understand dependency injection and control inversion
- . net tip: talk about the problem that the scoped service cannot be obtained in the middleware structure
- [MySQL] left Function | Right Function
猜你喜欢

Top ten project management software similar to JIRA

IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统

C read / write binary file

編碼電機PID調試(速度環|比特置環|跟隨)

OLED multi-level menu record

2022山东省安全员C证上岗证题库及在线模拟考试
![General test technology [II] test method](/img/b7/f661f446616ad6bfbbf48eb03ea82d.png)
General test technology [II] test method

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

软件测试相关知识~

2022g2 boiler stoker examination question bank and online simulation examination
随机推荐
软件测试相关知识~
Why is bi so important to enterprises?
超好用的Excel异步导出功能
Impact of AOT and single file release on program performance
Establishing and traversing binary tree
[vs Code] solve the problem that the jupyter file displays exceptions in vs code
This new feature of C 11, I would like to call it the strongest!
yes. Net future
Blazor University (12) - component lifecycle
MySQL port is occupied when building xampp
交换二叉树中每个结点的左和右
Using stack to solve the problem of "mini parser"
研讨会回放视频:如何提升Jenkins能力,使其成为真正的DevOps平台
一套组合拳,打造一款 IDEA 护眼方案
Find the number of leaf nodes of binary tree
C WPF UI framework mahapps switching theme
The most understandable life cycle of dependency injection
2022年做跨境电商五大技巧小分享
How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing
Docker拉取mysql并连接