This should be defined by your application-level communication protocol. (
http://en.wikipedia.org/wiki/Application_layer[
^].)
I would advise that the protocol should carry some redundancy and meta-data. The serialized data sent should better be "prefixed". For example, on the top of protocol, you are sending and receiving some variable number of data elements. Start it with sending the number of data elements receiving party should expect. Same thing goes for each data element and each part of it, hierarchically. When it comes to an individual string, first send the length of it (and probably some meta-data information like encoding of it). This way, each receiving party will maintain the integrity of each communication on every step.
—SA