🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.unix.programmer
4 messages
4 total messages Started by Marko Asplund Tue, 29 Sep 1998 00:00
C structs in heterogenous environments
#3294
Author: Marko Asplund
Date: Tue, 29 Sep 1998 00:00
26 lines
1619 bytes
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?


--
	aspa
Re: C structs in heterogenous environments
#3295
Author: 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.
Re: C structs in heterogenous environments
#3296
Author: Dan Pop
Date: Tue, 29 Sep 1998 00:00
72 lines
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
Re: C structs in heterogenous environments
#3297
Author: Marko Asplund
Date: Thu, 01 Oct 1998 00:00
45 lines
2135 bytes
Barry Margolin wrote:
> 
> 
> You can't really assume anything.  Besides the issues you raised, there's
> also the problem of byte order within the integers.

originally i planned on solving this with htonl and ntohl functions.

> 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).

yes, the XDR library routines seem suitable for my purposes. but a few
things about using the XDR routines are a bit unclear to me. for example
if i want to both send and receive data through a socket connection
using the XDR routines, the code probably looks something like this:

	...

	/* establishing the socket connection, error handling and XDR
filter 		           definition for struct netdata have been omitted */

	fp = fdopen(fd, "r+");

	xdrstdio_create(&xdrs, fp, XDR_DECODE);

	xdr_netdata(&xdrs, &data);

	xdrstdio_create(&xdrs, fp, XDR_ENCODE);

	xdr_netdata(&xdrs, &data);
	...


- do i have to fflush() the stream on the write end before trying to
read from it?

- do the xdr-routines e.g. xdrstdio_create() reserve resources (apart
from the stream allocated with fdopen()) that need to be freed? 

- when closing the connection are fclose() and close() equivalent?


--
	marko asplund
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads