c++ - 在 boost::asio 中发送/接收结构

标签 c++ boost boost-asio

我打算使用 boost::asio::async_write_some 从客户端向服务器发送一个结构,在本例中为 boost::serialization boost::property_tree 来帮忙,

//boost::serialization
struct blank
{
    int m_id;
    std::string m_message;

    template<typename archive>
    void serialize(archive& ar, const short version)
    {
        ar & m_id;
        ar & m_message;
    }
};

blank info;

info.m_id = 1;
info.m_name = "Rasul";

std::stringstream ss;
boost::archive::binary_oarchive out_archive(ss);

out_archive << info;

那么,现在我如何使用 boost::asio 异步发送/接收 out_archive .. 或者

//boost::property_tree
boost::property_tree::ptree root;
root.put("id", 2);
root.put("name", "Rasul");

如何使用 boost::asio 异步发送/接收 root??? (如果您有其他想法,请分享)

最佳答案

Ok,完全不清楚你尝试了什么或者问题是什么,所以你去吧:

使用 Boost 序列化

Live On Coliru

#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/asio.hpp>
#include <iostream>

//boost::serialization
struct blank
{
    int m_id;
    std::string m_message;

    template<typename archive> void serialize(archive& ar, const unsigned /*version*/) {
        ar & m_id;
        ar & m_message;
    }
};

void on_send_completed(boost::system::error_code ec, size_t bytes_transferred) {
    if (ec)
        std::cout << "Send failed: " << ec.message() << "\n";
    else
        std::cout << "Send succesful (" << bytes_transferred << " bytes)\n";
}

namespace ba = boost::asio;
using ba::ip::tcp;

struct IO {
    boost::asio::streambuf buf;

    void send_asynchronously(tcp::socket& socket) {
        blank info;

        info.m_id = 1;
        info.m_message = "Rasul";

        {
            std::ostream os(&buf);
            boost::archive::binary_oarchive out_archive(os);
            out_archive << info;
        }

        async_write(socket, buf, on_send_completed);
    }
};

int main() {
    ba::io_service ios;
    tcp::socket s(ios);
    s.connect({{},6868});

    IO io;
    io.send_asynchronously(s);

    ios.run();
}

当对抗例如netcat -l -p 6868 | xxd:

Send succesful (62 bytes)
00000000: 1600 0000 0000 0000 7365 7269 616c 697a  ........serializ
00000010: 6174 696f 6e3a 3a61 7263 6869 7665 0f00  ation::archive..
00000020: 0408 0408 0100 0000 0000 0000 0001 0000  ................
00000030: 0005 0000 0000 0000 0052 6173 756c       .........Rasul

boost 属性树+序列化

一些变化:

Live On Coliru

void send_asynchronously(tcp::socket& socket) {
    boost::property_tree::ptree root;
    root.put("id", 2);
    root.put("name", "Rasul");

    {
        std::ostream os(&buf);
        boost::archive::binary_oarchive out_archive(os);
        out_archive << root;
    }

    async_write(socket, buf, on_send_completed);
}

打印:

Send succesful (138 bytes)
00000000: 1600 0000 0000 0000 7365 7269 616c 697a  ........serializ
00000010: 6174 696f 6e3a 3a61 7263 6869 7665 0f00  ation::archive..
00000020: 0408 0408 0100 0000 0000 0000 0002 0000  ................
00000030: 0000 0000 0000 0000 0000 0000 0000 0200  ................
00000040: 0000 0000 0000 6964 0000 0000 0000 0000  ......id........
00000050: 0000 0000 0100 0000 0000 0000 3204 0000  ............2...
00000060: 0000 0000 006e 616d 6500 0000 0000 0000  .....name.......
00000070: 0000 0000 0005 0000 0000 0000 0052 6173  .............Ras
00000080: 756c 0000 0000 0000 0000                 ul........

关于c++ - 在 boost::asio 中发送/接收结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47236474/

相关文章:

c++ - 在AVX512中乘以不同类型

c++ - 使用容器将单个值解析为 ast 节点

c++ - 编译错误error C2039 : 'clock_t' : is not a member of '` global namespace''

c++ - static_cast 与 boost::lexical_cast

c++ - SSL 例程:SSL3_GET_RECORD:boost::asio::async_read 中解密失败错误

android - 了解 ImageProcessing 代码

c++ - C++14 中 main() 的合法定义

c++ - 有没有办法获取有关 boost::flyweight 内部容器的信息?

c# - 是否有类似 boost::asio for .NET (Mono) 的东西?

c++ - io_service::run() 的专用线程