How to serialize an object to send over network

前端 未结 5 1177
陌清茗
陌清茗 2021-02-15 17:42

I\'m trying to serialize objects to send over network through a socket using only STL. I\'m not finding a way to keep objects\' structure to be deserialized in the other host. I

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-15 18:13

    You can serialize with anything. All serialization means is that you are converting the object to bytes so that you can send it over a stream (like an std::ostream) and read it with another (like an std::istream). Just override operator <<(std::ostream&, const T&) and operator >>(std::istream&, T&) where T is each of your types. And all the types contained in your types.

    However, you should probably just use an already-existing library (Boost is pretty nice). There are tons of things that a library like Boost does for you, like byte-ordering, taking care of common objects (like arrays and all the stuff from the standard library), providing a consistent means of performing serialization and tons of other stuff.

提交回复
热议问题