Jump to content
  • Advertisement
Sign in to follow this  
nethackpro

recv in C++

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

Is there a way to get this function
recv(int s, void *buf, size_t len, int flags)
to work with C++ strings? The reason why I want to do this is because in my Player class I have
string Input;
I would like recv to be able to put the data straight into that. Ive been trying to get used to strings because I'm finding them much easier and nicer to work with then arrays of chars.

Share this post


Link to post
Share on other sites
Advertisement
C++/the socket API doesn't provide this, but it shouldn't be too hard to make a RecvString function or something.

The actual implementation of course depends on the format that is used to send the string over the network, for example, is it a null terminated sequence of characters? or a count, followed by that many characters?

Share this post


Link to post
Share on other sites
Quote:
Original post by quasar3d
C++/the socket API doesn't provide this, but it shouldn't be too hard to make a RecvString function or something.

The actual implementation of course depends on the format that is used to send the string over the network, for example, is it a null terminated sequence of characters? or a count, followed by that many characters?


What would be the most efficient way to do it? Theres no reason I couldnt have a count.

Share this post


Link to post
Share on other sites
Quote:
Original post by Antheus
Receive into some char array 'buf', the assign to string using assign(buf, length).


So simple... I dont know why I didnt think of it. Thanks!

Share this post


Link to post
Share on other sites
Quote:
Original post by nethackpro
Quote:
Original post by Antheus
Receive into some char array 'buf', the assign to string using assign(buf, length).


So simple... I dont know why I didnt think of it. Thanks!


Let me warn you the dangers of doing that without sanitizing your input. If the user sends data in a unicode format or non-ascii characters, your game server can get really messed up or your server will forward that data to other players and their clients can get messed up. Be sure to check the contents of 'buf' for correct ASCII data before you assign to a std::string and use that data!

I've seen an exploit like this done in a F2P online MMORPG causing everyone's client to simply freeze while it spun in circles trying to handle the invalid data. Improperly using the 'ischaracter' type of functions can also result in debug runtime asserts if you are passing negative values to the function for the character to send (i.e. > 127), so you will need to make sure to test against ascii instead.

I.e. if your server has logic like this:

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(int argc, char * argv[])
{
char data[3];
data[0] = 'A';
data[1] = 0x80;
data[2] = 0;
bool bValid = true;
for(int x = 0; x < strlen(data); ++x)
{
if(!isalnum(data[x]))
{
bValid = false;
break;
}
}
if(bValid)
{
// Handle string since it is valid
}
else
{
// Handle error
}
return 0;
}


It will cause a debug runtime assert. If you were testing your server in debug mode with players in an alpha, you definitely don't want that happening. If you use isascii instead first to check the character and then test for valid combination of character types, you can avoid that problem.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(int argc, char * argv[])
{
char data[3];
data[0] = 'A';
data[1] = 0x80;
data[2] = 0;
bool bValid = true;
for(int x = 0; x < strlen(data); ++x)
{
if(!isascii(data[x]))
{
bValid = false;
break;
}
if(!isalnum(data[x]))
{
bValid = false;
break;
}
}
if(bValid)
{
// Handle string since it is valid
}
else
{
// Handle error
}
return 0;
}


Not really the best code examples, but a few things to watch out for.

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!