提高:: ASIO async_read保证读取所有字节 [英] boost::asio async_read guarantee all bytes are read
问题描述
我有一个从客户端接收一个COM pressed串(COM pressed与zlib的)的服务器,而我是用 async_receive
从的boost :: ASIO
库获得这个字符串,原来不过是没有保证所有字节将被接收,所以我现在有将其更改为 async_read
。我面对的问题是,收到的字节大小是可变的,所以我不知道如何使用 async_read
不知道要接收的字节数。随着 async_receive
我只是有一个的boost ::数组<焦炭,1024>
,然而,这是一个缓冲区是不一定完全填充。
I have a server that receives a compressed string (compressed with zlib) from a client, and I was using async_receive
from the boost::asio
library to receive this string, it turns out however that there is no guarantee that all bytes will be received, so I now have to change it to async_read
. The problem I face is that the size of the bytes received is variable, so I am not sure how to use async_read
without knowing the number of bytes to be received. With the async_receive
I just have a boost::array<char, 1024>
, however this is a buffer that is not necessarily filled completely.
我想知道如果任何人都可以提出一个解决方案,我可以使用async_read即使我不知道的字节数提前收到?
I wondered if anyone can suggest a solution where I can use async_read even though I do not know the number of bytes to be received in advance?
void tcp_connection::start(boost::shared_ptr<ResolverQueueHandler> queue_handler)
{
if (!_queue_handler.get())
_queue_handler = queue_handler;
std::fill(buff.begin(), buff.end(), 0);
//socket_.async_receive(boost::asio::buffer(buff), boost::bind(&tcp_connection::handle_read, shared_from_this(), boost::asio::placeholders::error));
boost::asio::async_read(socket_, boost::asio::buffer(buff), boost::bind(&tcp_connection::handle_read, shared_from_this(), boost::asio::placeholders::error));
}
BUFF
是的boost ::数组&LT;焦炭,1024 GT;
推荐答案
您是怎样期待做到这一点使用任何其他方法?
How were you expecting to do this using any other method?
有一些通用的方法在异步发送庄园可变大小的数据:
There are a few general methods to sending data of variable sizes in an async manor:
- 将消息 - 这意味着你有一个定义预期的消息之后,其中包含指定长度的数据体长度的头
- 将流 - 这意味着你有一些标记(这是非常广阔)知道什么时候你得到一个完整的数据包的方法
- 将连接 - 每个数据完整的数据包中,一旦数据是完整而封闭的单一连接发送 。
因此,可以将您的数据进行分析,或长度发送等...
So can your data be parsed, or a length sent etc...
这篇关于提高:: ASIO async_read保证读取所有字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!