当前位置:网站首页>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;
}边栏推荐
- Reading notes of Clickhouse principle analysis and Application Practice (4)
- Summary of leetcode BFS question brushing
- InputStream/OutputStream(文件的输入输出)
- Arcpy uses the updatelayer function to change the symbol system of the layer
- Uniapp custom environment variables
- 2022 wechat enterprise mailbox login entry introduction, how to open and register enterprise wechat enterprise mailbox?
- Can the out of sequence message complete TCP three handshakes
- Tf/pytorch/cafe-cv/nlp/ audio - practical demonstration of full ecosystem CPU deployment - Intel openvino tool suite course summary (Part 2)
- 2022.7.2-----leetcode.871
- 双色球案例
猜你喜欢

C实现贪吃蛇小游戏

ABAP:OOALV实现增删改查功能

How to use multithreading to export excel under massive data? Source code attached!

C language - Blue Bridge Cup - Snake filling

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

Learning multi-level structural information for small organ segmentation

Fundamentals of SQL database operation

Practical gadget instructions

Tree DP

JSON Web Token----JWT和传统session登录认证对比
随机推荐
金盾视频播放器拦截的软件关键词和进程信息
8. Factory method
[number theory] fast power (Euler power)
Detectron: train your own data set -- convert your own data format to coco format
What is Gibson's law?
ABAP:OOALV实现增删改查功能
JSON web token -- comparison between JWT and traditional session login authentication
Sleep quality today 78 points
740. Delete and get points
运算符<< >>傻瓜式测试用例
Common JS tool Libraries
JS execution mechanism
如何实现视频平台会员多账号登录
11. Dimitt's law
High performance parallel programming and optimization | lesson 02 homework at home
How to implement cross domain requests
Common usage of time library
buuctf-pwn write-ups (8)
Fast power (template)
Tree DP