当前位置:网站首页>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;
}
边栏推荐
- Learning multi-level structural information for small organ segmentation
- Learning multi-level structural information for small organ segmentation
- 树形dp
- Overview of convolutional neural network structure optimization
- 8. Factory method
- Realize IIC data / instruction interaction with micro batg135
- What is the sheji principle?
- 2022 Xinjiang's latest eight members (Safety Officer) simulated examination questions and answers
- 17-18. Dependency scope and life cycle plug-ins
- R statistical mapping - random forest classification analysis and species abundance difference test combination diagram
猜你喜欢
How does apscheduler set tasks not to be concurrent (that is, execute the next task after the first one)?
Another company raised the price of SAIC Roewe new energy products from March 1
Arcpy uses the updatelayer function to change the symbol system of the layer
JSON Web Token----JWT和傳統session登錄認證對比
buuctf-pwn write-ups (8)
C language exercises (recursion)
Leetcode question brushing record | 206_ Reverse linked list
24 magicaccessorimpl can access the debugging of all methods
Appium foundation - appium installation (II)
[number theory] fast power (Euler power)
随机推荐
Another company raised the price of SAIC Roewe new energy products from March 1
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
[number theory] fast power (Euler power)
How does apscheduler set tasks not to be concurrent (that is, execute the next task after the first one)?
如何实现视频平台会员多账号登录
C语言练习题(递归)
Json Web token - jwt vs. Traditional session login Authentication
Inputstream/outputstream (input and output of file)
Operator < <> > fool test case
[openvino+paddle] paddle detection / OCR / SEG export based on paddle2onnx
Redis面试题集
Lightroom import picture gray / Black rectangular multi display
[March 3, 2019] MAC starts redis
4G wireless all network solar hydrological equipment power monitoring system bms110
[Chongqing Guangdong education] electronic circuit homework question bank of RTVU secondary school
leetcode 310. Minimum Height Trees
Weekly summary (*63): about positive energy
MySQL的information_schema数据库
1、 Relevant theories and tools of network security penetration testing
Webrtc quickly set up video call and video conference