简体   繁体   中英

Boost sockets - client is not receiving all bytes from server

I am developing an application with C++ and having some difficulty with boost sockets. The server sends an image but not all the bytes are received by the client; the client always receives about 500 bytes less than the server sent. Provided below is the pertinent code and screenshots of the program running.

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 total bytes sent by the server is divisible by 10000; this code is just for testing):

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 of server: 服务器截图

Screenshot of client: 客户端截图

Thanks for your time; any help or suggestions would be greatly appreciated.

There're no guarantee you'll receive complete buffers of 10000 bytes.

I'd recommend following approach:

To send some binary data w/o any explicit terminator, first send its size and only then data itself. In this case client will know how many data in this chunk it should receive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM