当前位置:网站首页>tcp socket 的 recv 如何接收指定长度消息?
tcp socket 的 recv 如何接收指定长度消息?
2022-07-04 06:33:00 【涛歌依旧】
有时候,tcp socket一定要接收到指定的字节数才算符合预期,那怎么实现呢?
在很多网络编程的书中都有介绍,一起来看看(一直循环接受,达标为止):
int recvLength(char *sRecvBuffer, size_t iRecvLen)
{
int iRet = checkSocket();
if(iRet < 0)
{
return iRet;
}
size_t iRecvLeft = iRecvLen;
iRecvLen = 0;
TC_Epoller epoller(false);
epoller.create(1);
epoller.add(_socket.getfd(), 0, EPOLLIN);
while(iRecvLeft != 0)
{
int iRetCode = epoller.wait(_timeout);
if (iRetCode < 0)
{
_socket.close();
return EM_SELECT;
}
else if (iRetCode == 0)
{
_socket.close();
return EM_TIMEOUT;
}
epoll_event ev = epoller.get(0);
if(ev.events & EPOLLIN)
{
int len = _socket.recv((void*)(sRecvBuffer + iRecvLen), iRecvLeft);
if (len < 0)
{
_socket.close();
return EM_RECV;
}
else if (len == 0)
{
_socket.close();
return EM_CLOSE;
}
iRecvLeft -= len;
iRecvLen += len;
}
else
{
_socket.close();
return EM_SELECT;
}
}
return EM_SUCCESS;
}
边栏推荐
- P26-P34 third_ template
- C语言中的排序,实现从小到大的数字排序法
- STM32 单片机ADC 电压计算
- 报错cvc-complex-type.2.4.a: 发现了以元素 ‘base-extension‘ 开头的无效内容。应以 ‘{layoutlib}‘ 之一开头。
- Summary of leetcode BFS question brushing
- Detectron: train your own data set -- convert your own data format to coco format
- Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
- 【MySQL】数据库视图的介绍、作用、创建、查看、删除和修改(附练习题)
- 2022.7.2-----leetcode.871
- ABAP:OOALV实现增删改查功能
猜你喜欢
云原生——上云必读之SSH篇(常用于远程登录云服务器)
C语言练习题(递归)
QT qtablewidget table column top requirements ideas and codes
How to use multithreading to export excel under massive data? Source code attached!
Review | categories and mechanisms of action of covid-19 neutralizing antibodies and small molecule drugs
SQL injection SQL lab 11~22
The solution of win11 taskbar right click without Task Manager - add win11 taskbar right click function
Overview of convolutional neural network structure optimization
buuctf-pwn write-ups (8)
Weekly summary (*63): about positive energy
随机推荐
The width of the picture in rich text used by wechat applet exceeds the problem
Sword finger offer II 038 Daily temperature
QT qtablewidget table column top requirements ideas and codes
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
Overview of convolutional neural network structure optimization
运算符<< >>傻瓜式测试用例
Inputstream/outputstream (input and output of file)
leetcode 310. Minimum Height Trees
What is the "relative dilemma" in cognitive fallacy?
Tsinghua University product: penalty gradient norm improves generalization of deep learning model
Which water in the environment needs water quality monitoring
Sleep quality today 78 points
High performance parallel programming and optimization | lesson 02 homework at home
Practical gadget instructions
Fast power (template)
ES6 模块化
Error CVC complex type 2.4. a: Invalid content beginning with element 'base extension' was found. Should start with one of '{layoutlib}'.
7. Agency mode
Review | categories and mechanisms of action of covid-19 neutralizing antibodies and small molecule drugs
双色球案例