当前位置:网站首页>How does the recv of TCP socket receive messages of specified length?
How does the recv of TCP socket receive messages of specified length?
2022-07-04 06:37:00 【Tao song remains the same】
occasionally ,tcp socket You must receive the specified number of bytes to meet the expectation , Then how to achieve ?
It is introduced in many books of network programming , Let's see ( Always accept , Up to standard ):
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;
}
边栏推荐
- Tree DP
- JSON Web Token----JWT和传统session登录认证对比
- Functions in C language (detailed explanation)
- What is the sheji principle?
- The solution of win11 taskbar right click without Task Manager - add win11 taskbar right click function
- Redis面试题集
- What is tweeman's law?
- How to help others effectively
- Native Cloud - SSH articles must be read on Cloud (used for Remote Login to Cloud Server)
- Invalid revision: 3.18.1-g262b901-dirty
猜你喜欢
Tree DP
Reading notes of Clickhouse principle analysis and Application Practice (4)
regular expression
C # symmetric encryption (AES encryption) ciphertext results generated each time, different ideas, code sharing
Arcpy 利用updatelayer函数改变图层的符号系统
R统计绘图-随机森林分类分析及物种丰度差异检验组合图
2022 where to find enterprise e-mail and which is the security of enterprise e-mail system?
SQL join, left join, right join usage
[problem record] 03 connect to MySQL database prompt: 1040 too many connections
R statistical mapping - random forest classification analysis and species abundance difference test combination diagram
随机推荐
C语言中的排序,实现从小到大的数字排序法
Data analysis notes 09
Realize IIC data / instruction interaction with micro batg135
Which water in the environment needs water quality monitoring
Fast power (template)
tars源码分析之3
Is the insurance annuity product worth buying? Is there a hole?
Inputstream/outputstream (input and output of file)
[number theory] fast power (Euler power)
Operator < <> > fool test case
Distributed cap theory
Sleep quality today 78 points
Redis面试题集
Json Web token - jwt vs. Traditional session login Authentication
采用中微BATG135实现IIC数据/指令交互
tars源码分析之8
How to avoid JVM memory leakage?
2022 wechat enterprise mailbox login entry introduction, how to open and register enterprise wechat enterprise mailbox?
Learning multi-level structural information for small organ segmentation
What is Gibson's law?