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

C++ 在boost::asio中发送/接收结构,c++,boost,boost-asio,C++,Boost,Boost Asio,我打算使用boost::asio::async\u write\u some将一个结构从客户端发送到服务器,在本例中,boost::serialization和boost::property\u tree来帮忙 //boost::serialization struct blank { int m_id; std::string m_message; template<typename archive> void serialize(archive&

我打算使用
boost::asio::async\u write\u some
将一个结构从客户端发送到服务器,在本例中,
boost::serialization
boost::property\u 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
异步发送/接收根目录???
(如果您有其他想法,请与他人分享)

好的,您尝试过什么或者问题出在哪里,现在还不清楚,请看:

使用Boost序列化

#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();
}
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);
}
Boost属性树+序列化 变化不大:

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

好的,现在还不清楚您尝试过什么,或者问题出在哪里,所以请看:

使用Boost序列化

#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();
}
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);
}
Boost属性树+序列化 变化不大:

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

伙计们,帮帮我,我真的需要你们的帮助你们试了什么?asio教程解释了如何异步发送数据。我尝试使用std::stringstream和boost::asio::streambuf发送我的结构,但遇到了一些问题,请大家帮助我,我真的需要你的帮助你尝试了什么?asio教程解释了如何异步发送数据。我尝试使用std::stringstream和boost::asio::streambuf发送我的结构,但出现了一些问题,添加了
属性树
序列化方法添加了
属性树
序列化方法