gpt4 book ai didi

c++ - 使用 boost iostream 每个连接发送多个文件

转载 作者:行者123 更新时间:2023-11-30 03:37:40 27 4
gpt4 key购买 nike

我正在尝试使用 iostream 的 boost 制作流式应用程序,但服务器没有分离图像帧在接收循环中,将所有内容都放在一个文件中(不关闭文件,并继续接收同一文件中的其他帧)。我能找到的唯一解决方案是发送一个连接帧,但流式传输速度非常慢。

当前每个连接发送 1 个文件并且一切正常(在远程网络上速度较慢)

我想将其更改为每个连接发送多个文件(我想我会 boost 性能),但我遇到了上述问题。

必须覆盖"/tmp/img.frame"

在我正在使用的代码下方(更改只是为了建立一个连接)

void send_()
{
boost::scoped_ptr<screenshot> ptr_screen(new screenshot);
handle_connection = true;

boost::asio::io_service svc;

boost::asio::ip::tcp::iostream stream_(boost::asio::ip::tcp::resolver::query{ "127.0.0.1", "6293" });

boost::iostreams::filtering_ostream out;
out.push(boost::iostreams::zlib_compressor());
out.push(stream_);

while (handle_connection) {
ptr_screen->Start(); // get screen.jpg

std::ifstream ifs("screen.jpg", std::ios::binary);
out << ifs.rdbuf();
out.flush();
ifs.close();
}
}


void receiver_()
{
connection_handle = true;
try
{
boost::asio::io_service io_service;

boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 6293);
boost::asio::ip::tcp::acceptor acceptor(io_service, endpoint);

boost::asio::ip::tcp::iostream stream;
boost::system::error_code ec;
acceptor.accept(*stream.rdbuf(), ec);

if(!stream) { return; }

boost::iostreams::filtering_istream in;
in.push(boost::iostreams::zlib_decompressor());
in.push(stream);

while(connection_handle)
{
std::ofstream ofs("/tmp/img.frame", std::ios::binary); // must be overwritten
copy(in, ofs);
ofs.close();
}
}
catch (std::exception& e)
{
std::cerr << "\n[-] " << e.what() << std::endl;
}
}

最佳答案

不管底层技术是什么,你都必须要实现TCP。是一种流式、无消息协议(protocol)。如果你想发送任何类型的单独消息并在后退端正确地分离它们,你必须实现某种应用程序协议(protocol):

  • 长度单词前缀
  • 类型-长度-值
  • STX/ETX,详细转义
  • 自描述协议(protocol),例如 XML

等等等等等

关于c++ - 使用 boost iostream 每个连接发送多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40086238/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com