使用 boost 序列化通过 boost-asio 套接字连接发送序列化类型
我试图将 1kb 的数据从“服务器”发送到“客户端”,但我就是无法正确发送。 在这方面我需要做一些事情: 1)需要使用boost-asio套接字来传输数据 2) 需要序列化我创建的类型(数据包),该类型将包含字符串或字符* 形式的数据
以下是发生的情况:
首先,我从服务器上的示例文本文件中获取 1kb 的数据。我得到这个并将其放入我创建的数据包类型中。我已在 Packet 中定义了数据字段,以将此数据保存为 std::string。 (我尝试了 char* 但效果不佳 - 请参阅下一段)。
其次,我使用 boost text_oarchive 对其进行序列化。如果数据包类型只包含一个字符串,那么我序列化数据包类型没有问题,但我真正想要的是一种使用数据类型为字符数组来序列化它的方法(以便它与下面的套接字配合得更好)
第三,我发送它通过 boost asio 插座。在这里我遇到了问题,因为我找不到通过套接字连接发送 std::string 的方法。我在示例和文档中看到的所有内容都需要使用某种类型的 char* 而不是字符串的缓冲区。
这只是一个头痛。你能帮忙吗?
I am trying to send 1kb of data from a "server" to a "client", but I just can't get it right.
There are a few things that I NEED to do in this:
1) Need to use boost-asio sockets to transfer the data
2) Need to serialize a type I created (Packet) that will contain the data as a string or char*
Here is what is going on:
First, I get 1kb of data from a sample text file on the server. I get this and put it into the Packet type that I created. I have defined the data field in Packet to hold this data as a std::string. (I tried char* but it didnt work well - see next paragraph).
Second I serialize it using boost text_oarchive . I have no problems serializing the Packet type if it just contains a string, but what I really want is a way to serialize it with the data type being a char array (so that it works better with the socket below)
Third, I send it over a boost asio socket. Here I have a problem because I can't find a way to send a std::string over the socket connection. Everything I see as examples and in the documentation need a buffer using some type of char* and not a string.
its just a headache. can you help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是正确的,尽管使用 Boost.Serialization 和 Boost.Asio 非常简单。您可以使用
text_oarchive
序列化为boost::asio::streambuf
,然后使用套接字发送生成的流缓冲区内容。请参阅此问题和我对该问题的回答以获得更完整的示例。
That is correct, though it's quite simple to do using Boost.Serialization and Boost.Asio. You can serialize using a
text_oarchive
to aboost::asio::streambuf
then send the resulting stream buffer contents using a socket.See this question and my answer to that question for a more complete example.