Why is boost :: asio :: read the size of the buffer data smaller than the read size?

I have a simple file transfer application that transfers 4096 bytes to the client for writing. At the end of the server, I use the following call:

tempLen = boost :: asio :: read (l_Socket, boost :: asio :: buffer (buf, bufSize), boost :: asio :: transfer_all (), error);

Templen is 1440 bytes, but when I read buf, it's only 11 bytes. Copy the server code below. I tried both socket.read_some and asio :: read - both ends in the same result. Can someone explain what I'm doing wrong here?

//boost::array<char, 4096> buf; char* buf = new char[4096]; char* bigBuffer = new char[2097152]; std::string strBuffer; strBuffer.clear(); for (;;) // Loop for the whole file length { boost::asio::read_until(l_Socket, request_buf, "\n\n"); std::istream request_stream(&request_buf); request_stream >> bufSize; std::cout<< "Size of the Compressed data transfer:" << bufSize << "\n"; // Clear the stream request_stream.clear(); memset(bigBuffer, 0, 2097152); memset(buf, 0, 4096); if(bufSize == 0) break; size_t len = 0, prevLen = 0, tempLen = 0; try{ //tempLen = l_Socket.read_some(boost::asio::buffer(buf, bufSize), error); tempLen = boost::asio::read(l_Socket, boost::asio::buffer(buf, bufSize), boost::asio::transfer_all(), error); std::cout << "Length from read: " << tempLen << " Buffer Size: " << bufSize << std::endl; prevLen = len; len += tempLen; } catch (boost::exception& e) { std::cerr << diagnostic_information(e); }.....} 

Edit:

Just verify that this problem only occurs when I send compressed data using the following function on the client.

  std::string CClient::Compress(const char* data, unsigned int* dataLen) { std::stringstream compressed; std::stringstream decompressed; std::cout << "From Compress Function: " << " Size of Decompressed Data: " << strlen(data) << std::endl; decompressed << data; boost::iostreams::filtering_streambuf<boost::iostreams::input> out; out.push(boost::iostreams::zlib_compressor()); out.push(decompressed); boost::iostreams::copy(out, compressed); *dataLen = compressed.str().size(); return compressed.str(); } std::string CClient::DeCompress(const std::string& data) { std::stringstream compressed; std::stringstream decompressed; compressed << data; boost::iostreams::filtering_streambuf<boost::iostreams::input> in; in.push(boost::iostreams::zlib_decompressor()); in.push(compressed); boost::iostreams::copy(in, decompressed); std::cout << "Decompressed Data: " << decompressed.str().c_str() << std::endl; return decompressed.str(); } 

When I decompress data from the "client" itself after compression (before sending), the data prints correctly. But when I read the data after getting on the server, I ran into this problem.

+4
source share
1 answer

The problem is the compression function. The line that is returned from the compression function when I convert it to the string c completes 11 bytes. I solved the problem by implementing a compression function to directly call zlib functions and process the data as a char string, rather than std :: string.

+1
source

Source: https://habr.com/ru/post/1400936/


All Articles