Re: packing a C structure
- From: rams <haairam@xxxxxxxxx>
- Date: Fri, 26 Oct 2007 01:56:20 -0700
On Oct 26, 1:18 pm, Josef Moellers <josef.moell...@fujitsu-
siemens.com> wrote:
rams wrote:
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,,
No offence, but my first name is "Josef". (We have these problems
ourselves with people from e.g. Japan ;-)
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...
As usual, TMTOWTDI:
if you have the socket open, e.g. in $sock, just do
*** Sender ***
my $data = join(':', $a, @b, $c, $d, @e, @f);
print $sock $data, "\n";
*** Receiver ***
my $data = <$sock>;
chomp $data;
my @data = split(/:/, $data);
$a = $data[0];
@b = @data[1..16];
$c = $data[17];
$d = $data[18];
@e = @data[19..24];
@f = @data[25..32];
You could also use some kind of tagging:
*** Sender ***
print $sock "a=$a\n";
foreach (0..15) {
print $sock "b$_=$b[$_]\n";}
...
print $sock "\n";
*** Receiver ***
while (<$sock>) {
if (/^a=(\d+)/) {
$a = $1;
} elsif (/^b(\d+)?(\d+)/) {
$b[$1] = $2;
} ... {
} elsif (/^\s+$/) {
last;
}
}
Maybe XML could be used.
You may also want to have a look at pack/unpack. They have data types
which are pretty well defined (e.g. L is an unsigned long of exactly 32
bits).
It's all a matter of
1. what you feel comfortable to use
2. what is most stable
3. what is best maintainable
4. what is fast
5. what takes little space
Obviously others will have more elegant solutions.
Josef
--
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 josef,,
sorry for the mis interpretation of your name....
actually the problem for me is i will not be able to modify the
reciever part...and the reciever part is a C code interpreting data in
binary mode....so i saw that pack functionality is used to send data
in binary format...the problem is iam not able to combine all the
elements in the structure into a single variable using the pack
format.... L is for long and but how to combine all those elements?..
Thanks & regards
Rams
.
- References:
- packing a C structure
- From: haairam
- Re: packing a C structure
- From: Josef Moellers
- Re: packing a C structure
- From: rams
- Re: packing a C structure
- From: Josef Moellers
- packing a C structure
- Prev by Date: Re: packing a C structure
- Next by Date: Re: packing a C structure
- Previous by thread: Re: packing a C structure
- Next by thread: Re: packing a C structure
- Index(es):
Relevant Pages
|
|