Jump to content
  • Advertisement
Sign in to follow this  
Demx

String problem

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

Hi ,
i got a struct and some code:

struct Player {

std::string name;

int x;
int y;
...
};

player p;

void main(){

char pname[20];
printf("Enter name :");
cin.getline (pname,20);

p.name = pname;

}

I then send the struct p to a server, but when the packet returns the string disapears (all other normal data sent and recieved is fine).

Can we send strings with sockets ?

thx for replys :)

Share this post


Link to post
Share on other sites
Advertisement
I'm not a network programmer but no, probably not.

A std::string is almost certainly represented internally as a pointer to memory containing the actual string so copying a string in binary form just copies the pointer which, on the recieving end, will point to nothing sensible.

I think to send strings across network, you need to use a char buffer of a preset size.

[EDIT] The mod's aren't keen on cross-posting here, although your other post is probably in a better forum.

Share this post


Link to post
Share on other sites
As Aardvajk said, std::string is allocating the string on the heap with an internal pointer, so you can't send the structure as it is.
What you need to do is to construct a Serialization function/method for your structure which packs the data so you are able to send through the socket, and once it has arrived you Deserialize it back to the structure.

Share this post


Link to post
Share on other sites
lol i didnt think it so hard just to send a message..

Isn't there a normal way to send a struct with a message from a socket to
another ?

If not , are there any simple guides to serialization ?

Share this post


Link to post
Share on other sites
Since you are already setting a maximum size for your name in the input function, just replace the std::string in your struct with a char[20] array.

Make sure you null append the text you put in the array, then send the entire array over the network.

In file i/o another solution is to prepend the string data with it's length but I'm not sure how this applies to sending network packets.

Share this post


Link to post
Share on other sites
k so now the struct is like this :

struct Player {

char name[20];
int x;
int y;
...
};
Player packet;


In the Main :

char pname[20];
printf("Enter name :");

cin >> pname;

// I copy pname to name in struct
for(i=0; pname!= '\0';i++){

packet.name=pname;

}



Then i send struct but its still not visible at on the other socket.. did i do something wrong ?

Share this post


Link to post
Share on other sites
Quote:
Then i send struct but its still not visible at on the other socket.. did i do something wrong ?


pname is still just a pointer to string data someplace else.

You need serialization. No, there is no magic way for it to sort itself out, you need to do it yourself.

Share this post


Link to post
Share on other sites
What you are doing is trying to avoid the problem and ignoring it.
Post more of your code including the transmission (within [ source ] tags)

Share this post


Link to post
Share on other sites
hi thanks for replys,
i solved the problem by copying the string in an array of int, the program then reconstructs the message based on ascii code.

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!