Boost sockets - the client does not receive all bytes from the server

I am developing a C ++ application and am having difficulty with socket support. The server sends the image, but not all bytes are accepted by the client; the client always receives 500 bytes less than the sent server. Below is the corresponding code and screenshots of the program.

Server Code:

int sent = boost::asio::write(*socket, response, boost::asio::transfer_all(), error); std::cout << "Sent: " << sent << std ::endl; 

Client code (I know that read_some will block if the shared bytes sent by the server are divided by 10000, this code is for testing only):

 int len = 0; int count = 0; do { len = socket->read_some( boost::asio::buffer( imageData, 10000 ) ); count += len; std::cout << "len: " << len << std::endl; std::cout << "count: " << count << std::endl; } while(len == 10000); std::cout << "Image Received of size: " << count << std::endl; 

Screenshot from server: Screenshothot of server

Client Screenshot: Screenshot of client

Thank you for your time; Any help or suggestions would be greatly appreciated.

+4
source share
1 answer

There is no guarantee that you will receive full buffers of 10,000 bytes.

I would recommend the following approach:

To send some binary data without any explicit terminator, first send its size, and then only the data. In this case, the client will know how much data in this piece he should receive.

+7
source

All Articles