开发者

sending a serialized type over a boost-asio socket connection using boost serialization

开发者 https://www.devze.com 2023-03-21 11:39 出处:网络
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:

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 parag开发者_JAVA百科raph).

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?


Everything I see as examples and in the documentation need a buffer using some type of char* and not a string

That is correct, though it's quite simple to do using Boost.Serialization and Boost.Asio. You can serialize using a text_oarchive to a boost::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.

0

精彩评论

暂无评论...
验证码 换一张
取 消