Sign in to follow this  

String problem

This topic is 2545 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
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[i]!= '\0';i++){

packet.name[i]=pname[i];

}



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
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
Quote:
Original post by Demx
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.

In C++, characters are just small integers. Your network code should be more robust than this anyway, to handle endianness/int size differences. You should write your network code to work regardless of these things, otherwise changing compilers (or even moving between compiler versions) could cause issues.

Are you sure you want to limit your game to ASCII characters?

Share this post


Link to post
Share on other sites
Quote:
Original post by Demx
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.


No, it sounds like you got your program to behave as you want, for now. You haven't solved anything because it sounds like you still have serious issues with your code.

Share this post


Link to post
Share on other sites
I don't really recommend sending structs across the network. It always looks like a nice idea, but it's much easier to just write bit/byte packing methods. For sending a string most people just prepend a length followed by the characters (UTF-8 ftw?). This means you can continue to use std::string if you want. Here's a quick article. Though there's a lot of people that still prefer structs.

Share this post


Link to post
Share on other sites

This topic is 2545 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this