boost::asio async_read guarantee all bytes are read

隐身守侯 提交于 2019-11-28 00:13:09

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:

  1. By message - meaning that you have a header that defines the length of the expected message followed by a body which contains data of the specified length.
  2. By stream - meaning that you have some marker (and this is very broad) method of knowing when you've gotten a complete packet.
  3. By connection - each complete packet of data is sent in a single connection which is closed once the data is complete.

So can your data be parsed, or a length sent etc...

Use async_read_until and create your own match condition, or change your protocol to send a header including the number of bytes to expect in the compressed string.

A single IP packet is limited to an MTU size of ~1500 bytes, and yet still you can download gigabyte-large files from your favourite website, and watch megabyte-sized videos on YouTube.

You need to send a header indicating the actual size of the raw data, and then receive the data by pieces on smaller chunks until you finish receiving all the bytes.

For example, when you download a large file over HTTP, there is a field on the header indicating the size of the file: Content-Length:.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!