mardi 1 août 2017

boost asio transfer_exactly reads 0 bytes

Given a sample server which transfers file contents over tcp -

#include <vector>
#include <fstream>
#include <iostream>
#include <boost/asio.hpp>

using namespace boost::asio;
using namespace boost::system;
using namespace boost::asio::ip;

using std::cerr;
using std::endl;
using std::ifstream;
using std::vector;
using std::ios;
using stream_iter = std::istreambuf_iterator<char>;

int main()
{
    ifstream input("movie.ogv", ios::in | ios::binary);
    vector<uint8_t> stream((stream_iter(input)), stream_iter());

    io_service ioservice;
    tcp::acceptor acceptor(ioservice, tcp::endpoint(tcp::v4(), 8000));
    tcp::socket socket(ioservice);
    acceptor.accept(socket);

    error_code ec;
    write(socket, boost::asio::buffer(stream), ec);
    if (ec) {
        cerr << ec.message() << endl;
    }
}

I wrote following client to extract starting header of file being transferred -

#include <vector>
#include <iostream>
#include <array>
#include <boost/asio.hpp>

using namespace boost::asio;
using namespace boost::system;
using namespace boost::asio::ip;

using std::cerr;
using std::endl;
using std::ifstream;
using std::vector;
using std::ios;
using stream_iter = std::istreambuf_iterator<char>;

int main()
{
    int header_size_ = 30000;
    error_code ec;
    vector<char> headerBytesBuf;
    headerBytesBuf.reserve(header_size_);
    io_service ioservice;
    tcp::socket socket(ioservice);
    socket.connect(tcp::endpoint(tcp::v4(), 8000));
    const auto bTransfer = read(socket, buffer(headerBytesBuf),
                                transfer_exactly(header_size_), ec);

    if (ec) {
        socket.close();
        std::cerr << ec.message();
    }

    std::cout << headerBytesBuf.size();
}

The relevant code is - const auto bTransfer = read(socket, buffer(headerBytesBuf), transfer_exactly(header_size_), ec); which reads 0 bytes into the vector while no error is recorded :/

Upon changing that line to -

std::array<char, 128> buf;
size_t total_transfer = 0;

while(total_transfer < header_size_) {
    size_t len = socket.read_some(boost::asio::buffer(buf));
    total_transfer += len;
    if (len <= 0) break;
}

The total_transfer reports 30800 which I can now trim and append to my vector within the loop. But why isn't the first method working here?

Aucun commentaire:

Enregistrer un commentaire