通过boost套接字发送和接收压缩文件 [英] Send and receiving compressed files with boost over socket

查看:98
本文介绍了通过boost套接字发送和接收压缩文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,使用boost的Zlib过滤器压缩套接字上的读写消息,我想知道如何对文件执行相同的操作.有什么更好的方法可以提高速度?将数据保存在缓冲区中而不使用硬盘?

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. what better way for better speed? Save the data in buffer without use hard disk?

我在使用boost传输文件时遇到了麻烦,因此欢迎任何帮助和示例.

I'm having trouble to transferring files using boost, so any help and examples are welcome.

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

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

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

I want to know how to compress a file without having to save a compressed copy in the 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:

在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, e.g.:

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

当它从c ++客户端接收到数据时,会愉快地报告"hello world".

Which happily reports "hello world" when it receives the data from the c++ client.

这篇关于通过boost套接字发送和接收压缩文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆