使用Boost iostream套接字读取和写入文件 [英] Reading and writing files with boost iostream socket

查看:96
本文介绍了使用Boost iostream套接字读取和写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用boost iostream套接字发送和接收文件.读取文件内容然后发送到流的最有效方法是什么?以及如何在服务器端读取此内容并写入文件?

I'm trying to send and receive files using boost iostream sockets. what is the most efficient way to read the contents of the file and then send to stream? And how to read this content on the server side and write to file?

发送:

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

std::ifstream fs;
fs.open("img.jpg", std::ios::binary);
sockstream << // send file to stream

接收:

boost::asio::io_service ios;

boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 3780);
boost::asio::ip::tcp::acceptor acceptor(ios, endpoint);

for (;;)
{
    boost::asio::ip::tcp::iostream stream;
    boost::system::error_code ec;
    acceptor.accept(*stream.rdbuf(), ec);

    if (!ec) {
        std::ofstream of;
        of.open("rcv.jpg", std::ios::binary);

        // read the file content with stream
        // write content to file
    }
}

推荐答案

我填写了文档示例中缺少的部分:

I filled in the missing pieces from the documentation example:

http://www.boost.org/doc/libs/1_62_0/doc/html/boost_asio/example/cpp03/iostreams/daytime_server.cpp

这是一个简单的发送程序/接收程序,(我认为)可以完成您期望的操作:

Here's a simple sender/receiver program that (I think) does what you expect:

在Coliru上直播

#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/asio.hpp>
#include <iostream>
#include <fstream>
using boost::asio::ip::tcp;

void sender() {
    boost::asio::io_service svc;

    tcp::iostream sockstream(tcp::resolver::query { "127.0.0.1", "6768" });

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

    {
        std::ifstream ifs("main.cpp", std::ios::binary); // pretend this is your JPEG
        out << ifs.rdbuf();
        out.flush();
    }
}

void receiver() {

    int counter = 0;
    try
    {
        boost::asio::io_service io_service;

        tcp::endpoint endpoint(tcp::v4(), 6768);
        tcp::acceptor acceptor(io_service, endpoint);

        for (;;)
        {
            tcp::iostream stream;
            boost::system::error_code ec;
            acceptor.accept(*stream.rdbuf(), ec);

            {
                boost::iostreams::filtering_istream in;
                in.push(boost::iostreams::zlib_decompressor());
                in.push(stream);

                std::ofstream jpg("test" + std::to_string(counter++) + ".out", std::ios::binary);
                copy(in, jpg);
            }

            // break; // just for shorter demo
        }
    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
        exit(255);
    }
}

int main(int argc, char**argv) {
    if (--argc && argv[1]==std::string("sender"))
       sender();
    else
       receiver();
}

运行接收器时:

./test

并多次使用发件人:

./test sender

接收器将解压缩并将接收到的文件写入test0.out,test1.out等.

The receiver will decompress and write the file received to test0.out, test1.out etc.

这篇关于使用Boost iostream套接字读取和写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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