🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.programmer
Article #3295

Re: C structs in heterogenous environments

#3295
From: Barry Margolin
Date: Tue, 29 Sep 1998 00:00
35 lines
2289 bytes
In article <3610F74A.A7A6BECD@mail.cern.ch>,
Marko Asplund  <aspa@mail.cern.ch> wrote:
>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? i assume i can rely on the C compiler to preserve the
>order of the struct members. but, i probably can't rely on int member
>fields being of certain length (at least not on all platforms). can i
>assume something about the paddings between members? for example, can i
>assume that if the char arrays lengths are multiples of 4, there will be
>no padding fields in most architecture?

You can't really assume anything.  Besides the issues you raised, there's
also the problem of byte order within the integers.

You should use a standard encoding mechanism.  Most Unix systems include
the XDR library routines, which implement the encoding mechanism used by
ONC RPC protocols (e.g. NFS).

-- 
Barry Margolin, barmar@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Technical Details
Message-ID: <jj7Q1.15$WG.127219@burlma1-snr1.gtei.net>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <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: Barry Margolin <barmar@bbnplanet.com>
Subject: Re: C structs in heterogenous environments
Date: 1998/09/29
Message-ID: <jj7Q1.15$WG.127219@burlma1-snr1.gtei.net>
X-Deja-AN: 396010724
Distribution: world
References: <3610F74A.A7A6BECD@mail.cern.ch>
X-Trace: burlma1-snr1.gtei.net 907084111 4.2.32.97 (Tue, 29 Sep 1998 15:48:31 GMT)
Organization: GTE Internetworking, Cambridge, MA
NNTP-Posting-Date: Tue, 29 Sep 1998 15:48:31 GMT
Newsgroups: comp.unix.programmer,cern.unix
Originator: barmar@bbnplanet.com (Barry Margolin)