如何从套接字读取中接收自定义数据类型? [英] How to receive a custom data type from socket read?

查看:76
本文介绍了如何从套接字读取中接收自定义数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想接收数据(自定义类型,例如CMYType) 我有以下代码;

I want to receive a data (of a custom type say CMYType) I have the following code;

using namespace boost::asio;

streambuf receivedStreamBuffer;
streambuf::mutable_buffers_type bufs;
bufs = receivedStreamBuffer.prepare( sizeof(CMYType) );
std::size_t length = read( *m_pReciver->ConSocket(), 
                           buffer(bufs, sizeof(CMYType)),  
                           transfer_all(),
                           ec);

关于如何将ReceivedStreamBuffer转换为CMYType类型的对象的任何想法吗?!

Any idea on how to convert receivedStreamBuffer to an object of type CMYType?!

推荐答案

请牢记其他人提到的可移植性和ABI警告.

Keep in mind portability and ABI caveats mentioned by others.

如果如您在评论中所述,您确定客户端和服务器在以下方面兼容:

If, as you say in a comment, you're sure your client and server are compatible in these ways:

只要您的类型满足POD要求,就可以使用buffer_cast:

Provided your type satisfies the POD requirements, you can use buffer_cast:

实际上,您可以使用buffer函数将其隐式化:

In fact you can have it implicit using the buffer function:

在Coliru上直播

#include <boost/asio.hpp>
#include <iostream>
#include <vector>
#include <boost/thread.hpp>

struct CMYType {
    float a,b,c;
};

static_assert(std::is_pod<CMYType>::value, "Not bitwise serializable");

static std::ostream& operator<<(std::ostream& os, CMYType const& cmy) {
    return os << "[" << cmy.a << "," << cmy.b << "," << cmy.c << "]";
}

static boost::mutex mx;

void server() {
    boost::system::error_code ec;
    using namespace boost::asio;

    io_service svc;
    ip::tcp::acceptor acc(svc, ip::tcp::endpoint{ {}, 6767 });

    {
        ip::tcp::socket sock(svc);
        acc.accept(sock, ec);

        CMYType data;
        std::size_t length = read(sock, buffer(&data, sizeof(data)), ec);
        boost::lock_guard<boost::mutex> lk(mx);
        std::cout << "length:" << length << " data: " << data << "\n";
    }

    {
        ip::tcp::socket sock(svc);
        acc.accept(sock, ec);

        std::vector<CMYType> data(10);
        std::size_t length = read(sock, buffer(data), ec);

        boost::lock_guard<boost::mutex> lk(mx);
        std::cout << "length:" << length << " data: { ";

        for(auto& cmy : data)
            std::cout << cmy << ", ";

        std::cout << " }\n";
    }
}

void client() {
    boost::system::error_code ec;
    using namespace boost::asio;

    io_service svc;

    {
        ip::tcp::socket sock(svc);
        sock.connect({ {}, 6767 }, ec);

        CMYType data { 1, 2, 3 };

        std::size_t length = write(sock, buffer(&data, sizeof(data)), ec);
        boost::lock_guard<boost::mutex> lk(mx);
        std::cout << "sent: " << length << " bytes\n";
    }

    {
        ip::tcp::socket sock(svc);
        sock.connect({ {}, 6767 }, ec);

        std::vector<CMYType> data { 
            { 1,  2,  3  }, { 4,  5,  6  }, { 7,  8,  9  }, { 10, 11, 12 }, { 13, 14, 15 },
            { 16, 17, 18 }, { 19, 20, 21 }, { 22, 23, 24 }, { 25, 26, 27 }, { 28, 29, 30 },
        };
        std::size_t length = write(sock, buffer(data), ec);

        boost::lock_guard<boost::mutex> lk(mx);
        std::cout << "sent: " << length << " bytes\n";
    }

}

int main() {
    boost::thread_group tg;
    tg.create_thread(server);
    tg.create_thread(client);

    tg.join_all();
}

打印

sent: 12 bytes
length:12 data: [1,2,3]
sent: 120 bytes
length:120 data: { [1,2,3], [4,5,6], [7,8,9], [10,11,12], [13,14,15], [16,17,18], [19,20,21], [22,23,24], [25,26,27], [28,29,30],  }

对于非POD数据

您可以使用例如增强序列化以序列化数据

For non-POD data

You can use e.g. Boost Serialization to serialize the data

这篇关于如何从套接字读取中接收自定义数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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