Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to build a simple application protocol using Berkeley sockets in C++ using on Linux. The transport layer should be UDP, and the protocols will contain the following two parts:

The first part:
It is a fixed part which represents the protocol Header with the following fields:
C++
1. int HeaderType
2. int TransactionID
3. unsigned char Source[4]
4. unsigned char Destination[4]
5. int numberoftlvs

The second part:
It will contain variable number of TLVs, each TLV will contain the following fields:
C++
1. int type
2. int length
3. unsigned char *data "Variable length"

My first question is:
For preparing the message to be sent over the wire, what's the best way to do serialization and deserialization, to be portable on all the systems like little Endian and big Endian?

Should I prepare a big buffer of "unsigned char", and start copying the fields one by one to it? And after that, just call send command?

If I am going to follow the previous way, how can I keep tracking the pointer to where to copy my fields, my guess would be to build for each datatype a function which will know how many bytes to move the pointer, correct?

If someone can provide me with a well explained example ,it will much appreciated.

Note: I just want to use the standard library of C++11, no other libraries like Boost.

My second question is:
What is the best way to represent an IP address in a datatype? Because representing it as a string will make it hard for portability for different languages.And it will take more space in the memory.
Posted

1 solution

I would advice considering boost serialization: http://www.boost.org/doc/libs/1_55_0/libs/serialization/doc/index.html[^].

This is the open source, of course. Please see also:
http://en.wikipedia.org/wiki/Boost_%28C%2B%2B_libraries%29[^],
http://www.boost.org/[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900