当前位置:网站首页>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
边栏推荐
- Find the number of leaf nodes of binary tree
- ASP. Net 6 middleware series - conditional Middleware
- Xamarin effect Chapter 21 expandable floating operation button in GIS
- Ide-idea-problem
- Queue storage and circular queue
- 12.<tag-链表和常考点综合>-lt.234-回文链表
- Comprehensive calculation of employee information
- Drawing polygons with < polygon / > circular array in SVG tag
- 2022A特种设备相关管理(电梯)上岗证题库及模拟考试
- 2022T电梯修理考试模拟100题及在线模拟考试
猜你喜欢

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

C语言实现通讯录----(静态版本)

Chapter 8 of C language programming (fifth edition of Tan Haoqiang) is good at using pointer exercises to analyze and answer

The whole network is the most complete. How to do interface automation test? Proficient in interface automation test details

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

The most easy to understand dependency injection and control inversion

OLED多级菜单记录

xutils3修改了我提报的一个bug,开心

2022年P气瓶充装培训试题及模拟考试

MySQL索引详解【B+Tree索引、哈希索引、全文索引、覆盖索引】
随机推荐
月薪10k-20k都无法回答的事务问题,你会吗?
类似Jira的十大项目管理软件
General testing technology [1] classification of testing
Queue storage and circular queue
Eight elder brothers chronicle [4]
The backtracking of stack is used to solve the problem of "the longest absolute path of file"
Use of ADB command [1]
Flink实时数仓项目—DWS层设计与实现
OLED多级菜单记录
ASP. Net and ASP NETCORE multi environment configuration comparison
Mysql database
Source generator actual combat
Use of metagroup object tuple in C
Fight leetcode again (290. Word law)
2022t elevator repair test simulation 100 questions and online simulation test
ASP. Net 6 middleware series - execution sequence
12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list
幂等性实践操作,基于业务讲解幂等性
編碼電機PID調試(速度環|比特置環|跟隨)
一文了解全面静态代码分析