Article View: comp.unix.programmer
Article #3296Re: C structs in heterogenous environments
From: Dan Pop
Date: Tue, 29 Sep 1998 00:00
Date: Tue, 29 Sep 1998 00:00
72 lines
2991 bytes
2991 bytes
In <3610F74A.A7A6BECD@mail.cern.ch> Marko Asplund <aspa@mail.cern.ch> writes:
>i have an application where i need to send C structs, with BSD socket
>interface's read() and write() routines, between heterogenous machine
>architectures. the structs are quite simple containing only char arrays
>and integers e.g.
>
> struct data {
> char somestring[9];
> int status;
> int foo;
> char otherstring[5];
> };
>
>
>so, my question is how do i accomplish sending this struct over the
>network, to a machine with different architecture? when i do a write()
>does the system send everything in the struct, including padding fields,
>over the network?
Yes.
>i assume i can rely on the C compiler to preserve the
>order of the struct members.
Yes.
>but, i probably can't rely on int member
>fields being of certain length (at least not on all platforms).
Correct.
>can i assume something about the paddings between members?
No.
>for example, can i
>assume that if the char arrays lengths are multiples of 4, there will be
>no padding fields in most architecture?
Probably, but there is no standard guaranteeing that. A 64-bit
platform might prefer to align everything on 8-byte boundaries.
If you're dealing with negative integer values you have the additional
problem that there is no unique representation for them required by the
C language.
You have to choose between restricting your application to platforms
satisfying a certain number of assumptions and biting the bullet and
writing code that doesn't rely on any assumption (other than a common
character set, which you need anyway for passing the strings).
If your strings don't contain any spaces, you can easily convert the
whole structure to a single string and send that string over the
network:
sprintf(buff, "%s %d %d %s", x.somestring, x.status, x.foo,
x.otherstring);
If your strings can contain spaces, then you have to use another character
as field separator.
At the receiving end, you read the string and convert it back to a struct,
with a similar sscanf() call. All you have to assume is ASCII everywhere,
which is a fair assumption for machines with a BSD socket interface.
Dan
--
Dan Pop
CERN, IT Division
Email: Dan.Pop@cern.ch
Mail: CERN - EP, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland
Technical Details
Message-ID:
<danpop.907084919@news.cern.ch>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3610F74A.A7A6BECD@mail.cern.ch>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fc89c,e19b5afbbfd1f181 X-Google-Attributes: gidfc89c,public X-Google-Thread: 1132ff,e19b5afbbfd1f181 X-Google-Attributes: gid1132ff,public From: Dan.Pop@cern.ch (Dan Pop) Subject: Re: C structs in heterogenous environments Date: 1998/09/29 Message-ID: <danpop.907084919@news.cern.ch> X-Deja-AN: 396034656 References: <3610F74A.A7A6BECD@mail.cern.ch> X-Complaints-To: news@sunnews.cern.ch X-Trace: sunnews.cern.ch 907086845 15202 (None) 137.138.32.79 Organization: CERN European Lab for Particle Physics Newsgroups: comp.unix.programmer,cern.unix