使用boost :: asio发送大文件

编程入门 行业动态 更新时间:2024-10-22 23:12:04
本文介绍了使用boost :: asio发送大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试从http服务器向一些客户端发送大文件(~4GB)。我的服务器实现基于Boosts HTTPServer3示例 HTTPServer3 。 问题是,文件首先完全加载到内存中然后发送给客户端。 我想读大约5-10mb的块并在读取下一个块之前发送它们,而不是将整个文件读入一个字符串(rep.content)。 这就是我的代码的样子:

Hi, I'm trying to send large files (~4GB) from a http server to some clients. My server implementation is based on Boosts HTTPServer3 Example HTTPServer3. The problem is, that the files are first completely loaded into memory and then send to the Client. I would like to read chunks of about 5-10mb and send them before reading the next chunk, instead of reading the whole file into one string (rep.content). Thats what my code looks like:

// Open the file to send back. std::ifstream is(full_path_no_params.c_str(), std::ios::in | std::ios::binary); if (!is) { rep = reply::stock_reply(reply::not_found); std::cout << "File not found\n"; return; } // Fill out the reply to be sent to the client. rep.status = reply::ok; // add content of file to reply char buf[512]; while (is.read(buf, sizeof(buf)).gcount() > 0) rep.content.append(buf, is.gcount()); rep.headers.resize(2); rep.headers[0].name = "Content-Length"; rep.headers[0].value = boost::lexical_cast<std::string>(rep.content.size()); rep.headers[1].name = "Content-Type"; rep.headers[1].value = mime_types::extension_to_type(extension);

使用boost :: asio :: async_write发送数据

The data is send using boost::asio::async_write

boost::asio::async_write(socket_, reply_.to_buffers(),strand_.wrap( boost::bind(&connection::handle_write, shared_from_this(), boost::asio::placeholders::error)));

和reply_的定义。 to_buffers()

and the definition of reply_.to_buffers()

std::vector<boost::asio::const_buffer> reply::to_buffers() { std::vector<boost::asio::const_buffer> buffers; buffers.push_back(status_strings::to_buffer(status)); for (std::size_t i = 0; i < headers.size(); ++i) { header& h = headers[i]; buffers.push_back(boost::asio::buffer(h.name)); buffers.push_back(boost::asio::buffer(misc_strings::name_value_separator)); buffers.push_back(boost::asio::buffer(h.value)); buffers.push_back(boost::asio::buffer(misc_strings::crlf)); } buffers.push_back(boost::asio::buffer(misc_strings::crlf)); buffers.push_back(boost::asio::buffer(content)); return buffers; }

任何想法怎么做? 我可以使用像升级一样的东西:: asio :: streambuf? Greets Chris

Any ideas how to do that?? May I use somesthing like boost::asio::streambuf? Greets Chris

推荐答案

看看这个在stackoverflow上回答。 考虑将数据拆分为网络数据包大小倍数的较小块,以最小化发送半空数据包。 1024的倍数可能最合适。 Take a look at this answer at stackoverflow. Consider splitting the data into smaller chunks in network packet size multiples to minize sending half empty packets. Multiples of 1024 may fit best.

更多推荐

使用boost :: asio发送大文件

本文发布于:2023-11-25 17:41:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630717.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大文件   boost   asio

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!