todd
todd

Reputation: 1286

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 server

Screenshot of client: Screenshot of client

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

Upvotes: 5

Views: 865

Answers (1)

Andriy Tylychko
Andriy Tylychko

Reputation: 16256

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.

Upvotes: 7

Related Questions