Thus, no, converting to ascii is not needed.
NOTE: this is a combination from what I've read and my own logic/knowledge.
If I am wrong, correct me
~Queasy.
Thus, no, converting to ascii is not needed.
NOTE: this is a combination from what I've read and my own logic/knowledge.
If I am wrong, correct me
~Queasy.
- Splat
What I do is create a header file in which a define my own fixed sized types...
typedef char int8;
typedef short int16;
typedef int int32;
typedef long long int64;
Then if I need to port to another platform I just need to change this header.
The only other thing that really differs between platforms is the byte order. There are some useful functions in winsock2.h (htons, htonl...) for swapping between native and TCP/IP byte ordering. These functions are also in BSD sockets so they will work with Linux. Don't know about other platforms though!
Does anyone know if there is a network byte order function for floats? I haven't seen any mention of such beast.
I was reading some docs on the design of XShipWars, and they use ascii text for their packets, with the claim that it solves porting issues...
But I'd much prefer to go with binary data, as it's more compact, and you can compute the size of a packet based on the data type sizes.
Thanks for the reply : )
I'm aware that most (all?) c/c++ compilers pad structures to 2/4/8 byte boundries, to speed memory access. Do compilers usually provide a way to prevent this from occuring, for specific structures? When I was using DJGPP, there was a #pragma you could use for individual structs, and I believe the gnu compiler has a command-line directive to prevent padding for ALL structures. Is there a similar directive or pragma for individual structs for gnu? What about MS Visual C++?
Obviously, this could greatly simplify my network code, and still prevent sending padding over a socket.
Thanks
-g
[This message has been edited by genovov (edited November 17, 1999).]
#pragma pack(push, original)
#pragma pack(1)
...
#pragma pack(pop, original)
- Splat
- Splat
* Define explicit data types (int8, int16, int32) and avoid implicit types like int.
* Align data to natural boundaries (16 bit types at even addresses) and avoid implicit padding by the compiler (even if you have to add your own padding).
* Pick a byte ordering and stick to it. I prefer big endian, but it's trivial to just define macros that do the right thing by platform and use them everywhere.
* Translate the data once, as it's imported, recv'd, or read and exported, sent, or written. All data structures in memory should be in native format, all data on disk should be in portable format.