Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

BlitzKrieg

Network byte order

This topic is 5829 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

If i am transferring data between little endian and big endian computers, i need to call htons() etc. on my shorts and longs. I am assuming i would still need to do this even if i am converting everything to an array of bytes before sending it? If so, how do i get a correct byte order for float''s, double''s etc. that aren''t longs or shorts? Or do i just have to make sure that if i am going to use data other than only bytes shorts and longs, i will only run the program on little endian systems? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Just do some casting magic:

float Float;
long NetworkFloat;

NetworkFloat = htonl( *((long *)(&Float)) );

You can do it with unions too (which might be cleaner).

Share this post


Link to post
Share on other sites
You shouldn''t be sending floats across the network in the first place. The bit layout of floats can vary a lot between different machines, so basically you can either scale your values and send them as integers (of course, that only works if your floats aren''t really _floating_ point), or you will have to deal with the per-CPU-type internal layout of floats. Good luck.

cu,
Prefect

Return to the Shadows

Share this post


Link to post
Share on other sites
Thats ok, i have a friend who knows ASM! haha
Thanks for that, hopefully the majority of the programs will be run on x86 machines, if it''s neccessary to run it on something else we''ll cross that bridge when we come to it.
Thanks,
CYer, Blitz

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!