Boost ASIO read X bytes synchroniously into a vector

Posted by xeross on Stack Overflow See other posts from Stack Overflow or by xeross
Published on 2010-04-29T14:23:01Z Indexed on 2010/04/29 14:27 UTC
Read the original article Hit count: 173

Filed under:
|

Hey,

I've been attempting to write a client/server app with boost now, so far it sends and receives but I can't seem to just read X bytes into a vector.

If I use the following code

vector<uint8_t> buf;

for (;;)
{
 buf.resize(4);
 boost::system::error_code error;

 size_t len = socket.read_some(boost::asio::buffer(buf), error);

 if (error == boost::asio::error::eof)
  break; // Connection closed cleanly by peer.
 else if (error)
  throw boost::system::system_error(error); // Some other error.
}

And the packet is bigger then 4 bytes then it seems it keeps writing into those 4 bytes until the entire packet has been received, however I want it to fetch 4 bytes, then allow me to parse them, and then get the rest of the packet.

Can anyone provide me with a working example, or at least a pointer on how to make it work properly ?

Regards, Xeross

© Stack Overflow or respective owner

Related posts about c++

Related posts about boost