Boost logo

Boost Users :

Subject: [Boost-users] asio::ip::tcp iostream wrong byte count read for last packet
From: Steve Coleman (Steve.Coleman_at_[hidden])
Date: 2014-08-08 12:58:05


I just started out with Boost and was writing a little binary file
transfer app and ran into a little problem, so I would appreciate if
someone could kindly point out what I am doing wrong here.
        
My problem is in getting the correct byte count read in the final packet
received. My last frame always returns the size of the buffer, not the
number of bytes read into the buffer. Isn't iostream::gcount(() meant
for this? If not, what is the correct 'Boost' way of doing this?

int spool_binary_data()
{
    boost::asio::ip::tcp::endpoint
        dataServerEndpoint(boost::asio::ip::tcp::v4(),
        PMMS_TRANSFER_PORT_NUMBER);
    boost::asio::ip::tcp::acceptor acceptor(dataServer,
        dataServerEndpoint);
    boost::asio::ip::tcp::iostream dataServerStream;
    acceptor.accept(*dataServerStream.rdbuf());
        
    char buffer[2048];
    std::streamsize nbytes = sizeof(buffer);
        
    FILE * pFile = fopen ("binary.out","w");
        
    // count = getavailable(this->native_socket) ??
    // stream->rdbuf()->available() ??
        
    while ( dataServerStream.read((char*)&buffer, nbytes) )
    {
        int bytes_read = dataServerStream.gcount(); // always 2048
        std::cout << "bytes read " << bytes_read << std::endl;
        fwrite(buffer,1,bytes_read,pFile);
    }
    dataServerStream.close();
    fclose(pFile);
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net