I have a client that sends me data using this function:
void CServerRetrieve::Send(char *buf, DWORD size, int flags)
{
unsigned char *zlib;
unsigned long szzlib;
m_zlib.Deflate((unsigned char*)buf, size + 1, &zlib, &szzlib); // include the terminating 0 char
char zbuf[5];
zbuf[0] = 'Z';
memcpy(&zbuf[1], &szzlib, 4);
send(m_Socket, zbuf, 5, flags);
send(m_Socket, (char*)zlib, szzlib, flags);
delete [] zlib;
}
I want to receive this data using Boost::asio, however I am not sure what type of buffer I should pass to socket.async_receive in order for it to receive this data?
I have tried a std::vector<char> and std::vector<std::string>, however no data is ever received in my buffer?
Can someone assist me as to what I’m doing wrong?
void tcp_connection::start()
{
socket_.async_receive(boost::asio::buffer(buff), boost::bind(&tcp_connection::handle_read, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
void tcp_connection::handle_read(const boost::system::error_code& err, size_t bytes_transferred)
{
if (!err || err == boost::asio::error::message_size)
{
size_t sz = buff.size(); //always 0!
}
}
The fact that you are receiving compressed data really does not matter to Boost.Asio. Assuming you you know the size of the data you are about to receive, a
std::vector<char>is fine for receiving the compressed data. You’ll need toresizeit prior to invokingasync_receiveJust make sure this buffer does not go out of scope until the completion handler is invoked. This concept is explained int theasync_readdocumentation.