Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
528 views
in Technique[技术] by (71.8m points)

c++ - sending/receiving a struct in boost::asio

I was going to send a struct from a client to a server using boost::asio::async_write_some, in this case boost::serialization and boost::property_tree come to help,

//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;

So, now how can I send/receive out_archive using boost::asio asynchronously .. or

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

How can I send/receive root using boost::asio asynchronously??? (If you have some other ideas please share them)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok, it's not clear at all what you've tried or what the problem is, so here you go:

Using Boost Serialization

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() << "
";
    else
        std::cout << "Send succesful (" << bytes_transferred << " bytes)
";
}

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();
}

When running against e.g. 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 Property Tree + Serialization

Few changes:

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

Prints:

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........

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...