使用Boost.Asio的获得"全包" [英] Using Boost.Asio to get "the whole packet"

查看:125
本文介绍了使用Boost.Asio的获得"全包"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TCP客户端连接到我的服务器,发送原始数据包。如何使用Boost.Asio的,我可以得到全数据包每次(异步,当然)?假设这些数据包可以是任意大小达到我的记忆的全尺寸。

I have a TCP client connecting to my server which is sending raw data packets. How, using Boost.Asio, can I get the "whole" packet every time (asynchronously, of course)? Assume these packets can be any size up to the full size of my memory.

基本上,我想避免创建静态大小的缓冲区。

Basically, I want to avoid creating a statically sized buffer.

推荐答案

通常,当你做异步IO,您的协议应该支持。
一个简单的办法是preFIX一个字节数组在逻辑层面它的长度,而且有阅读code缓冲区,直到它有一个完整的缓冲准备解析。

typically, when you do async IO, your protocol should support it. one easy way is to prefix a byte array with it's length at the logical level, and have the reading code buffer up until it has a full buffer ready for parsing.

如果你不这样做,你将最终获得这个逻辑分散各地的地方(想想看空结尾的字符串,这意味着什么,如果你只是得到它的一部分,每次选择/ poll返回)。

if you don't do it, you will end up with this logic scattered all over the place (think about reading a null terminated string, and what it means if you just get a part of it every time select/poll returns).

这篇关于使用Boost.Asio的获得"全包"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆