Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serialize an object to send over network

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 tried converting to string, to char* and I've spent a long time searching for tutorials on the internet and until now I have found nothing.

Is there a way to do it only with STL?

Are there any good tutorials?

I am almost trying boost, but if there is how to do it with STL I'd like to learn.

like image 282
Lucas Arbiza Avatar asked Jul 16 '10 01:07

Lucas Arbiza


People also ask

What happens if you try to send non serialized object over network?

What happens if you try to send non-serialized Object over network? When traversing a graph, an object may be encountered that does not support the Serializable interface. In this case the NotSerializableException will be thrown and will identify the class of the non-serializable object.

What is serialization in networking?

To wit, serialization is the process of converting a data object into a byte stream, and saving the state of the object to be stored on a disk or transmitted across a network. This cuts down the storage size needed and makes it easier to transfer information over a network.

Can a serialized object be transferred over a network?

Using serialization, an object can be transferred across domains through firewalls, as well as be used for different languages and platforms. The formats of serialized objects are standardized so as to be able to be read by different platforms, if needed.


2 Answers

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.

like image 113
Travis Gockel Avatar answered Oct 05 '22 01:10

Travis Gockel


My first question will be: do you want serialization or messaging ?

It might seem stupid at first, since you asked for serialization, but then I have always distinguished the two terms.

  • Serialization is about taking a snapshot of your memory and restoring it later on. Each object is represented as a separate entity (though they might be composed)
  • Messaging is about sending information from one point to another. The message usually has its own grammar and may not reflect the organization of your Business Model.

Too often I've seen people using Serialization where Messaging should have been used. It does not mean that Serialization is useless, but it does mean that you should think ahead of times. It's quite difficult to alter the BOM once you have decided to serialize it, especially if you decide to relocate some part of information (move it from one object to another)... because how then are you going to decode the "old" serialized version ?

Now that that's been cleared up...

... I will recommend Google's Protocol Buffer.

You could perfectly rewrite your own using the STL, but you would end up doing work that has already been done, and unless you wish to learn from it, it's quite pointless.

One great thing about protobuf is that it's language agnostic in a way: ie you can generate the encoder/decoder of a given message for C++, Java or Python. The use of Python is nice for message injection (testing) or message decoding (to check the output of a logged message). It's not something that would come easy were you to use the STL.

like image 30
Matthieu M. Avatar answered Oct 05 '22 02:10

Matthieu M.