Re: structs for data transfer?

From: nospam (nospam_at_example.com)
Date: 09/20/04


Date: Sun, 19 Sep 2004 18:22:48 -0400

Oliver Gerlich wrote:
> I have doubts about this (because of data packing, and because
> sizeof(bool) might be inconsistent between compilers...), but I'm not
> sure...

Yes you are right :-)
1. You can not be sure not only about bool, but also about byte order(endian
type) and int size.
2. Worst than that, there may be alignament difference into the elements of
the stuct.

What you have to do is to "serialize" data for sending and "deserialize" for
receiving. Doing that means to define some rules about how each data type
is converted into a byte array and back.

> Can someone tell me if this concept is right or wrong? Or has someone a
> better solution for this problem (maybe something else than structs)?

You can always send a structure safelly, after you have defined a method of
serialization of that struct :-)

If you have time, a good book to read it will be Stevens "Unix Network
Programming" for a clear ideea about sockets.



Relevant Pages

  • Re: C++ Class Datatype.
    ... It has pointers, structure data type, ... C++ allows structs to have functions as members, ... The compiler can generate whatever it sees fit. ... I doubt that any C++ compilers add virtual functions to plain old data type ...
    (comp.parallel.mpi)
  • Re: Serializing a glib data type
    ... I want to serialize a glib data type (an hash), ... access directly to the fields of the various structs, ...
    (comp.lang.c)