我有一臺服務器從客戶端接收一個壓縮字符串(用zlib壓縮),並且我使用boost::asio
庫中的async_receive
來接收這個字符串,但事實證明,不能保證所有字節都會被接收到,所以我現在必須將其更改爲async_read
。我面臨的問題是收到的字節大小是可變的,所以我不知道如何使用async_read
而不知道要接收的字節數。與async_receive
我只是有一個boost::array<char, 1024>
,但是這是一個緩衝區,不一定完全填滿。boost :: asio async_read保證所有字節被讀取
我想知道是否有人可以建議一個解決方案,我可以使用async_read,即使我不知道預先收到的字節數?
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::array<char, 1024>
+1對一些可能的解決方案的不錯總結 – 2011-02-08 14:37:36