There are many ways to achieve that, but having some header is a good idea. Generally, all communications are better in prefixed form. Say, you have a message with variable size. Naturally, it's the best to tell the size first, the the reader would read just enough bytes. Same thing is with the messages of variable content. You may have some field which the reader receives first, and then, based on this information as on
descriptor, the reader knows the expected structure of the rest of this message, and it can be done hierarchically.
Your question is way to vague to discuss it in further detail, but I hope you get the idea.
However, all such considerations might be too low-level for the application. The thing is: the whole complex of such problem is already solved for you. If you really want to use raw sockets, not WCF, not .NET remoting, you can use your socket streams with pre-serialized data. In other forms, using of
serialization will have you to exchange your programming
objects instead of raw data, which will be automatically deserialized on the reading end.
Please see:
http://en.wikipedia.org/wiki/Serialization[
^],
http://msdn.microsoft.com/en-us/library/7ay27kt9%28v=vs.110%29.aspx[
^].
In many cases, I would recommend to use the best serialization (at least in some aspects) offered in .NET FCL —
Data Contract:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[
^].
Please see my past answers:
How can I utilize XML File streamwriter and reader in my form application?[
^],
Creating property files...[
^],
deseralize a json string array[
^].
—SA