Maximum portability

Started by
10 comments, last by GameDev.net 17 years, 9 months ago
Hey all... I'm still working on my network layer and one question comes to mind : If I want to make sure linux / windows clients can communicate with each other, sending positions, velocities etc. Must I do something with the incoming data structs ( char* ) or the cast on the remote side will work if its composed only of floats,long and shorts ? I would like your opinion on what is the best way to stay crossplatform. Thanks !
Advertisement
Quote:cast on the remote side will work if its composed only of floats,long and shorts ?

Theres a few things you need to take into account.
Struct padding :you need to make sure that the settings you use on the are the same or both the nix and win. This can be acheived using #pragma's (slighly different for win and nix so they are best guarded by defines)
Data type size: Floats and doubles are standard size yet longs and shorts are not, so you need to use the same data type ie. Uni8.

Other than this there should not be a problem.
Quote:Original post by Anonymous Poster
Data type size: Floats and doubles are standard size yet longs and shorts are not, so you need to use the same data type ie. Uni8.



So if I send longs or shorts, what is the best way to do so ?

I think you're missing his point. It's not a question of whether you should use long or short - use neither. You need to use a type that has a well-defined size. e.g. the standard C99 header stdint.h defines types such as int16_t which is always a 16-bit integer, regardless of platform.

To illustrate, "long" is always 32-bit when using MSVC, but it is 32-bit in gcc when targeting 32-bit platforms and 64-bit when targeting 64-bit platforms.
-Mike
Thanks, I didnt knew about the standard C99 stdint.h

I'll search on this :)
Ok after I searched a bit, I found that I needed files for these types...

I downloaded inttypes.h stdint.h endian.h

but when I try to compile, it tell me type redefinition in io.h ... but if I only include io.h, i cant have types like int16_t

I'm on Windows by the way. Any help would be appreciated.

Thanks

fwiw this works for me on windows + linux

#ifdef _WIN32

typedef char int8;
typedef unsigned char uint8;
typedef short int16;
typedef unsigned short uint16;
typedef int int32;
typedef unsigned int uint32;
typedef __int64 int64;
typedef unsigned __int64 uint64;

#else

typedef char int8;
typedef unsigned char uint8;
typedef short int16;
typedef unsigned short uint16;
typedef int int32;
typedef unsigned int uint32;
typedef long long int64;
typedef unsigned long long uint64;

#endif
Thanks ! :D
Lookup the functions named "ntohl()", "htonl()", "htons()" and "ntohs()".
Quote:Original post by Anonymous Poster
Lookup the functions named "ntohl()", "htonl()", "htons()" and "ntohs()".


I know these functions, should I convert data to network byte order in my structs before sending them with winsock ? or by using types of the same size on both OS it should be ok ?

This topic is closed to new replies.

Advertisement