Jump to content
  • Advertisement
Sign in to follow this  
K-Zodron

Problem with recv (Winsock)

This topic is 4834 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! I'm using Winsock for networking in my game, and to fix that 2 or more messages won't pack into one, I put a size infront every message and so on... Well, the problem is that sometimes I only get the 2 first bytes of the real message. If the size is 10, it works fine, and most other sizes work aswell, but some of them just *dong*, nemo, only 2 first bytes... Hope you get what I mean :p Here's the code:
n = recv(theSocket, (char*)buffer, 2, 0);
	
int size = atoi(buffer);
    
n = recv(theSocket, (char*)buffer+2, size, 0);

...

return buffer;

Share this post


Link to post
Share on other sites
Advertisement
What do you mean with "Null terminating string"?

And I found out a weird thing; the whole shit works fine when it's about client -> server >.>

Edit: I checked the n, it returns the same value as size, meaning it seems to be alright, maybe some problems with the string then?

Share this post


Link to post
Share on other sites
Woho!

It seems to work fine now, the declaration of Buffer was a bit weird (char buffer[50000], changed to char buffer [1024])

Thanks for the help guys!

Share this post


Link to post
Share on other sites
Hello Zodron! :)

A "null terminated string" is an array of char:s ending with zero, 0x0. The 0x0, or NULL character, as it is referred to, tells the function that is using it where the string ends.

Hint: Look at the otserv code to do this (It's in networkmessage.cpp)

Good luck!

Share this post


Link to post
Share on other sites
Quote:
Original post by K-Zodron

n = recv(theSocket, (char*)buffer, 2, 0);

int size = atoi(buffer);

n = recv(theSocket, (char*)buffer+2, size, 0);

...

return buffer;


Just as a warning... what will you do if that first recv doesn't manage to read 2 bytes? What about if the second recv doesn't read 'size' bytes? If you're using TCP, this could be a problem as you're not guaranteed to get all of your message made available in one go.

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!