Sivaram Kannan
Sivaram Kannan

Reputation: 121

Why is boost::asio::read buffer data size lesser than read size?

I have a simple file transfer application, which transfers 4096 bytes per write from the client. In the server end, I am using the following call read

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

The templen is 1440 bytes, but when I read the buf, it is only 11 bytes. Copy pasting the server code below. I have tried both socket.read_some and asio::read - both end in same result. Can someone explain what am I 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 checked this problem is happening only when I send the data compressed with the following function at 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 the data at "the client" itself after compression(before sending), the data is getting printed properly. But when I read the data after receiving at the server, I am facing this problem.

Upvotes: 4

Views: 1621

Answers (1)

Sivaram Kannan
Sivaram Kannan

Reputation: 121

The problem seems to be in the compression function. The string that gets returned from the Compression function, when I convert it in to c string, it terminates 11 bytes. I solved the problem by implementing the compression function to directly call zlib functions and handling data as char string rather than std::string.

Upvotes: 1

Related Questions