简体   繁体   English

通过boost over socket发送和接收压缩文件

[英]Send and receiving compressed files with boost over socket

In my project, read and write messages over socket are compressed with Zlib Filters of boost, i would like to know how to do the same with files . 在我的项目中,使用boost的Zlib过滤器压缩套接字上的读写消息,我想知道如何对files进行相同操作 what better way for better speed? 有什么更好的方法可以提高速度? Save the data in buffer without use hard disk? 不使用硬盘将数据保存在缓冲区中?

I'm having trouble to transferring files using boost, so any help and examples are welcome. 我在使用boost传输文件时遇到了麻烦,因此欢迎任何帮助和示例。

I am using the following code to send compressed messages: 我正在使用以下代码发送压缩消息:

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'));

EDIT: 编辑:

I want to know how to compress a file without having to save a compressed copy in the HD. 我想知道如何压缩文件而不必在HD中保存压缩的副本。 Saving your bytes directly into a buffer and then send to the socket. 将字节直接保存到缓冲区中,然后发送到套接字。 Server-side must receive the file and decompress 服务器端必须接收文件并解压缩

Here's the simplest thing I can think of that does what it appears you're trying to do: 这是我能想到的最简单的事情,它可以完成您尝试做的事情:

Live On Coliru 生活在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();
}

Use a simple command line listener, eg: 使用简单的命令行侦听器,例如:

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

Which happily reports "hello world" when it receives the data from the c++ client. 当它从c ++客户端接收数据时,哪个会愉快地报告“ hello world”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM