Reading data from a network connection without making assumptions about bit representation

39 Views Asked by At

I want to transfer data from a client to a server. The data is stored in a Data:

class Data{
     Type1 Obj1;
     Type2 Obj2;
     Type3 Obj3;
     //...
}

First I insert the data into a buffer type:

Buffer(data);

Then I use one of Boost.Asio's send functions:

Send(endpoint_s, buffer_1)

Alternatively, I could send Data directly:

Send(endpoint_s, &data, sizeof(Data));

On the receiving end, I am expecting a Data. First I receive the data into a Buffer.

Receive(endpoint_c, buffer_2);

Now buffer_2 contains a collection of bits that represents a Data on the server's machine. I wish I could just reinterpret_cast these bits into a Data, however, this results in undefined behavior. How am I supposed to convert the collection of bits in buffer_2 into a Data in a way that does not result in undefined behavior?

0

There are 0 best solutions below