使用Boost.Asio获得“整个分组” [英] Using Boost.Asio to get "the whole packet"

查看:135
本文介绍了使用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,你的协议应该支持它。
一个简单的方法是在逻辑级别为字节数组加上它的长度前缀,并使读取代码缓冲区上升,直到它有一个完整的缓冲区准备好进行解析。

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.

如果你不这样做,你会得到这个逻辑分散在所有的地方(考虑读取一个空终止字符串,这意味着如果你只是得到它的每一次的一部分select / 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天全站免登陆