Vanilla List Mailing List Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [VANILLA-LIST:3131] Binary portability
Dave Ahn writes:
>
> Packets are constructed in network byte order but stored (and saved to
> file) in host byte order. When this file is read by another machine,
> there may be an extra byteswap that is needed.
To deal with this byteswap (the netrek client/server already does this,
the cambot may not), when your program wants to turn a native variable
into something standard for writing to a socket or saving to a portable
binary file, use the htonl() family of calls (host to network long (or s
for short etc)).
When your program reads from a socket or portable file, convert it to
native with the converse ntohl() etc.
On machines where the native format happens to be the same endianness as
the "network" standard, these calls do nothing. Compile the same code
on some other machines, and the calls convert happily. This makes it
easy to write portable code.
Alec
--
Alec Habig, Boston University Particle Astrophysics Group
habig@budoe.bu.edu
http://hep.bu.edu/~habig/