通过boost asio iostream下载大文件的最快方法是什么?

时间:2020-05-20 13:27:08

标签: c++ boost boost-asio

我正在尝试通过boost :: asio :: ip :: tcp :: iostream像这样下载/传输大文件:

boost::asio::ip::tcp::iostream stream("127.0.0.1", "1234");
stream << "GET /data HTTP/1.0\r\n\r\n" << std::flush;
std::string text;
while (std::getline(stream, text)) {
    // pass, no operation here
}

但是,代码需要3秒钟以上的时间才能在本地计算机上下载400MB文件,这对于 localhost 文件传输来说太慢了。谁能给我任何有关如何加快速度的建议?

1 个答案:

答案 0 :(得分:2)

如果您真的(?)想要/ dev /清空数据,这是一个黑客:

boost::asio::ip::tcp::iostream stream("127.0.0.1", "1234");
stream << "GET /data HTTP/1.0\r\n\r\n" << std::flush;
std::ostream ons(nullptr);
ons << stream.rdbuf();

如果确实要下载文件,请阅读直到标题为止:

for (std::string text; std::getline(stream, text);)
    if (text.empty())
        break; // end of headers

然后逐块读取正文:

char buf[2048];
while (stream.read(buf, sizeof(buf)) || stream.gcount()) {
    // do something with gcount() bytes in buf?
}

高级HTTP意识

当然,HTTP是善变的野兽。它可以使用压缩,分块编码,保持活动状态等。这很容易导致您读取损坏的正文。为了更安全,请使用Beast做家务:

int main() {
    net::io_context io;
    tcp::socket s(io);
    s.connect({{}, 1234});

    std::string const& req = "GET /data HTTP/1.0\r\n\r\n";
    net::write(s, net::buffer(req));

    http::request<http::string_body> response;
    beast::flat_buffer buf;
    http::read(s, buf, response);
}

为了获得更多的了解,为什么不以相同的方式撰写/发送请求:

{
    http::request<http::empty_body> req;
    req.method(http::verb::get);
    req.target("/data");
    req.version(10);
    http::write(s, req);
}

完整列表

Live On Coliru

#include <boost/beast.hpp>
#include <boost/beast/http.hpp>

namespace net   = boost::asio;
namespace beast = boost::beast;
namespace http  = beast::http;
using net::ip::tcp;

int main() {
    net::io_context io;
    tcp::socket s(io);
    s.connect({{}, 1234});

    {
        http::request<http::empty_body> req;
        req.method(http::verb::get);
        req.target("/data");
        req.version(10);
        http::write(s, req);
    }

    {
        http::request<http::string_body> response;
        beast::flat_buffer buf;
        http::read(s, buf, response);
    }
}
相关问题