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

标签 c++ boost iostream

我正在尝试使用 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/

相关文章:

c++ - 使用 Boost Spirit Classic 解析 SQL INSERT

c++ - 用户定义类的输入流

c++ - 为什么在调用更复杂的cin和cout时C++ iostream重载失败?

c++ - C++动态调用函数的方法

c++ - 如何正确设置 ios 标志以进行流操作?

c++ - 我应该考虑网络字节顺序吗?

与 Ubuntu 中的 CPP 应用程序兼容的 PHP 信号量

boost - 为什么 Boost::Filesystem 有二进制部分?

c++ - 如何正确使用 boost::make_shared_ptr?

java - 从 C++ 中 java 的 writeInt 方法写入的文件中读取一个 int?