当前位置:网站首页>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;
}边栏推荐
- How to realize multi account login of video platform members
- Sleep quality today 78 points
- Overview of convolutional neural network structure optimization
- C语言中的排序,实现从小到大的数字排序法
- 【无标题】
- 测试岗的中年危机该如何选择?是坚守还是另寻出路?且看下文
- 1、 Relevant theories and tools of network security penetration testing
- Json Web token - jwt vs. Traditional session login Authentication
- Review | categories and mechanisms of action of covid-19 neutralizing antibodies and small molecule drugs
- AWT common components, FileDialog file selection box
猜你喜欢

buuctf-pwn write-ups (8)

regular expression

InputStream/OutputStream(文件的输入输出)

如何实现视频平台会员多账号登录

24 magicaccessorimpl can access the debugging of all methods

How to choose the middle-aged crisis of the testing post? Stick to it or find another way out? See below

C language - Blue Bridge Cup - Snake filling
![[untitled]](/img/32/cfd45bb5e8555ea2ad344161370dbe.png)
[untitled]

Layoutmanager layout manager: flowlayout, borderlayout, GridLayout, gridbaglayout, CardLayout, BoxLayout

buuctf-pwn write-ups (8)
随机推荐
[number theory] fast power (Euler power)
Dimension and format of data
[MySQL] introduction, function, creation, view, deletion and modification of database view (with exercises)
Arcpy uses the updatelayer function to change the symbol system of the layer
Experience weekly report no. 102 (July 4, 2022)
实用的小工具指令
Arcpy 利用updatelayer函数改变图层的符号系统
Average two numbers
云原生——上云必读之SSH篇(常用于远程登录云服务器)
What is the "relative dilemma" in cognitive fallacy?
《ClickHouse原理解析与应用实践》读书笔记(4)
C realize Snake games
Invalid revision: 3.18.1-g262b901-dirty
746. Climb stairs with minimum cost
Software keywords and process information intercepted by Golden Shield video player
2022.7.3-----leetcode. five hundred and fifty-six
C语言练习题(递归)
Lightroom import picture gray / Black rectangular multi display
Another company raised the price of SAIC Roewe new energy products from March 1
Redis面试题集