【问题标题】:Send and receiving compressed files with boost over socket使用 boost over socket 发送和接收压缩文件
【发布时间】:2016-10-07 01:30:50
【问题描述】:

在我的项目中,通过 socket 读取和写入的消息是用 boost 的 Zlib 过滤器压缩的,我想知道如何对 files 做同样的事情。 有什么更好的方法来提高速度?不使用硬盘将数据保存到缓冲区?

我在使用 boost 传输文件时遇到问题,欢迎提供任何帮助和示例。

我正在使用以下代码发送压缩消息:

std::string data_
std::stringstream compressed;
std::stringstream original;

original << data_;

boost::iostreams::filtering_streambuf<boost::iostreams::input> out;
out.push(boost::iostreams::zlib_compressor());
out.push(original);
boost::iostreams::copy(out, compressed);

data_ = compressed.str();
boost::asio::write(socket_, boost::asio::buffer(data_ + '\n'));

编辑:

我想知道如何压缩文件而不必在 HD 中保存压缩副本。将字节直接保存到缓冲区中,然后发送到套接字。服务端必须接收文件并解压

【问题讨论】:

  • 能否详细说明您遇到的问题?什么不起作用?

标签: c++ boost


【解决方案1】:

这是我能想到的最简单的事情,它看起来就像你想做的那样:

Live On Coliru

#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/asio.hpp>
#include <iostream>

int main() {
    std::string data_ = "hello world\n";
    boost::asio::io_service svc;

    using boost::asio::ip::tcp;
    tcp::iostream sockstream(tcp::resolver::query { "127.0.0.1", "6767" });

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

    out << data_;
    out.flush();
}

使用简单的命令行监听器,例如:

netcat -l -p 6767 | zlib-flate -uncompress

当它收到来自 c++ 客户端的数据时,它会愉快地报告“hello world”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 2016-11-29
    • 2013-06-09
    • 1970-01-01
    相关资源
    最近更新 更多