Jump to content
  • Advertisement
Sign in to follow this  
arcantos9104

How to read the message?

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

Hello all, my first post here, and I'm in a bit of a quandary. I'm working on a multiplayer game, where integers are sent over the network. My question is whether or not 4 bytes will be sent no matter what the number is, and if so would you typecast 4 bytes back to an integer?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by arcantos9104
My question is whether or not 4 bytes will be sent no matter what the number is
Assuming you send it as a four-byte number, yes.
Quote:
and if so would you typecast 4 bytes back to an integer?
Sure, though you'd want to worry about byte ordering issues.

Share this post


Link to post
Share on other sites
if you use TCP, be careful as there is the possibility you will receive the 4 bytes integer in a fragmented fashion (for example, the first call of recv() will return the first three bytes, a second call will return you the last byte).

Share this post


Link to post
Share on other sites
Assuming the server and clients have the same endian architecture.

Potential for clients to have both big/little at same time....

Share this post


Link to post
Share on other sites
Never assume types.

"I sent 4 bytes."
"I read 4 bytes."

But what do those 4 bytes mean? float? 32-bit int? 32-bit uint?

"I send 1 byte type and then 4 bytes"
"I read 1 byte to know how to read the next n bytes"

It's all about your protocol handling.

Obviously if you're going "offset-based" then you can skip the
typing information, but then you kind of limit yourself in terms of flexibility in order to save a few bytes of traffic (which is completely valid in many cases.)
Lots of protocols will have "predefined messages" which specific what offsets are what types.

"I sent a login message of 6 bytes."
"I read 1 byte length, 1 byte login-message-id, 4 bytes uint32 userid and I know how to parse it because we've agreed on what a login message is.

Also, be aware of htons/l() and ntohs/l() variations (byte ordering.)

-Szii

Share this post


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

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!