• Advertisement
Sign in to follow this  

Maximum portability

This topic is 4260 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 !

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Lookup the functions named "ntohl()", "htonl()", "htons()" and "ntohs()".

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by zppz

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


personally I would use something like:

//types
#ifdef _WIN32
typedef signed __int8 int8_t;
typedef unsigned __int8 uint8_t;
typedef signed __int16 int16_t;
typedef unsigned __int16 uint16_t;
typedef signed __int32 int32_t;
typedef unsigned __int32 uint32_t;
#else//*nix
# include <cstdint>
#endif// types

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by md_lasalle
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 ?



If you are planning to use the app across machines with different endianess(is that a word?) then I would advise you always converted you data into a standard endian(ie big); thus ensures that any machine which recieves data knows that the data is in a constant form and knows if and how to convert to the machine endianess(there it is again :) ).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement