我有一个如下所示的数据结构:
<前>类型定义结构{无符号短 m_short1;无符号短 m_short2;无符号字符 m_character;我的数据类型;我想使用 boost::serialization 来序列化这个数据结构,然后使用 boost::asio 通过 TCP/IP 传输它,然后让另一个应用程序接收数据并使用相同的 boost 库反序列化它.
我正在尝试关注 boost::serialization教程,(正如其他一些 SO 问题所建议的那样) 但该示例专门用于写入/读取文件,而不是使用 boost::asio 的套接字.
我很确定我有适合这项工作的工具——我只是需要帮助让它们协同工作.写入套接字与写入文件不会有什么不同,对吗?
非常感谢任何建议.谢谢!
对于如此简单的结构,boost::serialization 是矫枉过正,开销巨大.
做得更简单:
vector净(3,0);net[0]=htons(data.m_short1);net[1]=htons(data.m_short2);net[2]=htons(data.character);asio::async_write(socket,buffer((char*)&net.front(),6),callback);向量净(3,0);asio::async_read(socket,buffer((char*)&net.front(),6),callback);打回来:data.m_short1=ntohs(net[0]);data.m_short2=ntohs(net[1]);data.character=ntohs(net[2]);
并为自己节省 boost::serialization 带来的巨大开销
如果您使用具有相同字节顺序的计算机工作的私有协议(大/小)只是按原样发送结构——POD.
I have a data structure that looks like this:
typedef struct { unsigned short m_short1; unsigned short m_short2; unsigned char m_character; } MyDataType;
I want to use boost::serialization to serialize this data structure, then use boost::asio to transmit it via TCP/IP, then have another application receive the data and de-serialize it using the same boost libraries.
I'm trying to following boost::serialization tutorial, (as some other SO questions have suggested) but the example is specifically for writing/reading to a file, not to a socket using boost::asio.
I'm pretty sure I've got the right tools for the job -- I just need help making them work together. Writing to a socket can't be that different from writing to a file, right?
Any suggestions are very much appreciated. Thanks!
For such simple structure, boost::serialization is overkill and huge overhead.
Do simpler:
vector<uint16_t> net(3,0);
net[0]=htons(data.m_short1);
net[1]=htons(data.m_short2);
net[2]=htons(data.character);
asio::async_write(socket,buffer((char*)&net.front(),6),callback);
vector<uint16_t> net(3,0);
asio::async_read(socket,buffer((char*)&net.front(),6),callback);
callback:
data.m_short1=ntohs(net[0]);
data.m_short2=ntohs(net[1]);
data.character=ntohs(net[2]);
And Save yourself HUGE overhead that boost::serialization has
And if you private protocol where computers with same order of bytes work (big/little) that just send structure as is -- POD.
这篇关于使用Boost序列化和发送数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!