当前位置:网站首页>socket編程 send()與 recv()函數詳解
socket編程 send()與 recv()函數詳解
2022-04-23 03:16: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://yzsam.com/2022/04/202204230315331299.html
边栏推荐
- Using stack to solve the problem of "mini parser"
- 2022年度Top9的任务管理系统
- 7-11 rearrange the linked list (25 points)
- 一套组合拳,打造一款 IDEA 护眼方案
- Using positive and negative traversal to solve the problem of "the shortest distance of characters"
- How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing
- 超好用的【通用Excel导入功能】
- 【无标题】
- A comprehensive understanding of static code analysis
- Student achievement management
猜你喜欢
ASP. Net 6 middleware series - execution sequence
General test technology [II] test method
[MySQL] left Function | Right Function
2022山东省安全员C证上岗证题库及在线模拟考试
The backtracking of stack is used to solve the problem of "the longest absolute path of file"
[new version release] componentone added Net 6 and blazor platform control support
How to achieve centralized management, flexible and efficient CI / CD online seminar highlights sharing
The most easy to understand service container and scope of dependency injection
[vs Code] solve the problem that the jupyter file displays exceptions in vs code
[Mysql] LEFT函数 | RIGHT函数
随机推荐
MySql分组查询规则
Ide-idea-problem
Blazor University (12) - component lifecycle
幂等性实践操作,基于业务讲解幂等性
建立与遍历二叉树
ASP. Net 6 middleware series - execution sequence
12.<tag-链表和常考点综合>-lt.234-回文链表
[mock data] fastmock dynamically returns the mock content according to the incoming parameters
Peut recevoir plusieurs paramètres de type de données - paramètres variables
再战leetcode (290.单词规律)
The most understandable life cycle of dependency injection
Queue storage and circular queue
全新的ORM框架——BeetlSQL介绍
交换二叉树中每个结点的左和右
MySQL installation pit
ThreadLocal 测试多线程变量实例
Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis
软件测试相关知识~
[Mysql] LEFT函数 | RIGHT函数
[untitled]