简体   繁体   English

Boost套接字 - 客户端没有从服务器接收所有字节

[英]Boost sockets - client is not receiving all bytes from server

I am developing an application with C++ and having some difficulty with boost sockets. 我正在使用C ++开发一个应用程序,并且在使用boost套接字时遇到了一些困难。 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. 客户端总是收到比服务器发送的少500个字节。 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): 客户端代码(我知道如果服务器发送的总字节数可以被10000整除,read_some将阻塞;此代码仅用于测试):

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. 无法保证您将收到10000字节的完整缓冲区。

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. 在这种情况下,客户端将知道它应该接收的这个块中有多少数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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