Re: avoid structure padding



pmm wrote:
i am trying out a UDP packet
transfer between a windows machine and a linux I created a structure on
both sides (ie on linux and on windows) and I sent using UDP
but on the receiving side when I am trying to reform the packet back
into structure tis giving me different outputs I assume it is the
problem of structure padding ...

Internet packets should always, first, have a clear layout spec
and second, be constructed and interpreted by functions that map
between that layout and whatever data format the program finds
most convenient. (This is often called "marshalling" and
"unmarshalling" the data.) For your code that does this to be
portable, i.e. function the same regardless of platform, it
cannot make any assumptions about C data formats other than
what is guaranteed by the standard. In that context, the safest
way to code such marshalling/unmarshalling functions is to copy
*8 bits at a time* of each integer value between the structure
members and the packet data buffer (treated as a byte array,
byte-sequentially transmitted on the network). There are quite
a few pitfalls involved in doing that, so if at all possible
you should use an existing, tested data marshalling library.
Sun's XDR spec was among the earliest such approaches. These
days, sometimes, XML format is used, although it requires a lot
more bandwidth than the information content being conveyed.
--
comp.lang.c.moderated - moderation address: clcm@xxxxxxxxxxxx -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
.