【问题标题】:Send multiple files per connection with boost iostream使用 boost iostream 每个连接发送多个文件
【发布时间】:2017-02-26 10:17:02
【问题描述】:

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

当前每个连接发送 1 个文件,一切正常(在远程网络上缓慢)

我想将其更改为每个连接发送多个文件(我认为我会在性能方面有所提升),但我遇到了上述问题。

“/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;
    }
}

【问题讨论】:

    标签: c++ boost iostream


    【解决方案1】:

    无论底层技术如何,您都必须意识到 TCP。是一种流式、无消息协议。如果您想发送任何类型的单个消息并在后退端正确地将它们分开,您必须实现某种应用程序协议:

    • 长度词前缀
    • 类型-长度-值
    • STX/ETX,带有转义的完整细节
    • XML 等自描述协议

    等等。等等等等。

    【讨论】:

      猜你喜欢
      • 2011-05-28
      • 1970-01-01
      • 2011-06-25
      • 2015-10-04
      • 1970-01-01
      • 1970-01-01
      • 2019-11-17
      • 2020-05-17
      • 1970-01-01
      相关资源
      最近更新 更多