Re: packing a C structure



On Oct 26, 12:06 pm, Josef Moellers <josef.moell...@fujitsu-
siemens.com> wrote:
haai...@xxxxxxxxx wrote:
hi ,,,

iam trying to pack a C structure to send via socket..i would like to
know how to frame the template for the C structure..

this is my structure

typedef struct buffer_t {
uint32_t a;
char b[16];
uint32_t c;
uint32_t d;
char e[6];
char f[8];
} buffer_t;

kindly suggest me the format for the pack function call..this will
help me to solve my issue...

Transferring binary structures is inherently non-portable, so there is
not a single way to deduce the actual binary layout of this structure,
as the alignment and even the format of the binary numbers (e.g.
endianness) will vary from machine type to machine type and even between
compilers. It might even be impossible to transfer the data if the
receiving machine is unable to handle 32 bit data, but I guess that's
less of a problem.
That's why techniques like ASN.1 were developed.

If the amount of data is relatively small and you can live with some
time spent in conversion, a simple (and more portable (*)) means to
transfer this data is to transform it into ascii, with suitable
separators, e.g. commas or colons, terminated by a line feed.

(*) Sheesh, I sure hope Scott Nudds doesn't read this ;-) It's "more"
portable because you have removed alignment and endianness problems,
there's still a portability issue in that the receiving machine might
speak EBCDIC while the sending machine speaks ASCII.
--
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details:http://www.fujitsu-siemens.com/imprint.html- Hide quoted text -

- Show quoted text -

hi Mollers,,
Thanks for the reply.iam an beginner in perl ..still not clear how to
convert the structure into an asciii string...i was stuck with this
issue for past 2 weeks..kindly throw some guidelines for this...

regards
Rams

.



Relevant Pages

  • Re: packing a C structure
    ... iam trying to pack a C structure to send via socket..i would like to ... char b; ... Sheesh, I sure hope Scott Nudds doesn't read this;-) It's "more" portable because you have removed alignment and endianness problems, there's still a portability issue in that the receiving machine might speak EBCDIC while the sending machine speaks ASCII. ...
    (comp.lang.perl.misc)
  • Re: Array of strings
    ... > pointer which is not guaranteed by the standard. ... > Recommending non-portable code without clearly stating it is ... >>> The code is portable if used with a char type. ... you see that I made a full disclosure on the code's portability ...
    (comp.lang.c)
  • Re: packing a C structure
    ... char b; ... endianness) will vary from machine type to machine type and even between ... there's still a portability issue in that the receiving machine might ... if you have the socket open, e.g. in $sock, just do ...
    (comp.lang.perl.misc)
  • Re: parsing a command from a config file
    ... *)0 is required for portability' was. ... arguments to execlwould you say are portable solutions? ... What 'a compatible pointer type' is exactly ... in an example in the text, so the assumption that char * is one is ...
    (comp.unix.programmer)
  • Re: Syslog : maximum length of string
    ... Josef Moellers wrote: ... that sends big string to syslog: ... char buf; ...
    (comp.os.linux.development.apps)