当前位置:网站首页>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;
}
边栏推荐
- InputStream/OutputStream(文件的输入输出)
- QT 获取随机颜色值设置label背景色 代码
- SQL join, left join, right join usage
- C语言中的排序,实现从小到大的数字排序法
- Layoutmanager layout manager: flowlayout, borderlayout, GridLayout, gridbaglayout, CardLayout, BoxLayout
- 17-18. Dependency scope and life cycle plug-ins
- Experience weekly report no. 102 (July 4, 2022)
- SQL injection SQL lab 11~22
- What is the sheji principle?
- Leetcode question brushing record | 206_ Reverse linked list
猜你喜欢
QT qtablewidget table column top requirements ideas and codes
Layoutmanager layout manager: flowlayout, borderlayout, GridLayout, gridbaglayout, CardLayout, BoxLayout
Abap:ooalv realizes the function of adding, deleting, modifying and checking
Detailed explanation of common APIs for component and container containers: frame, panel, scrollpane
R统计绘图-随机森林分类分析及物种丰度差异检验组合图
Arcpy 利用updatelayer函数改变图层的符号系统
Cloud native - SSH article that must be read on the cloud (commonly used for remote login to ECS)
How to use multithreading to export excel under massive data? Source code attached!
Detectron: train your own data set -- convert your own data format to coco format
Review | categories and mechanisms of action of covid-19 neutralizing antibodies and small molecule drugs
随机推荐
JSON Web Token----JWT和傳統session登錄認證對比
buuctf-pwn write-ups (8)
How does apscheduler set tasks not to be concurrent (that is, execute the next task after the first one)?
Webrtc quickly set up video call and video conference
Invalid bound statement (not found): com. example. mapper. TblUserRecordMapper. login
17-18. Dependency scope and life cycle plug-ins
C语言练习题(递归)
Sleep quality today 78 points
uniapp 自定义环境变量
Reading notes of Clickhouse principle analysis and Application Practice (4)
Is the insurance annuity product worth buying? Is there a hole?
Distributed cap theory
STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
Tsinghua University product: penalty gradient norm improves generalization of deep learning model
Software keywords and process information intercepted by Golden Shield video player
Displaying currency in Indian numbering format
雲原生——上雲必讀之SSH篇(常用於遠程登錄雲服務器)
C实现贪吃蛇小游戏
2022 where to find enterprise e-mail and which is the security of enterprise e-mail system?
JSON web token -- comparison between JWT and traditional session login authentication