Jump to content
  • Advertisement
Sign in to follow this  
X5-Programmer

Send Vector Lists.

This topic is 4878 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 just wounder if its possible to send vector Lists and recv them? Can I do something like this?
//Simple Struct.

struct DATA
{
	char name[16];
};

vector<struct DATA> pData_send;

//Send
DATA data;
strcpy(data.name, "test");

pData_send.push_back( data );

send(mySocket, (char*)&pData_send, sizeof(DATA);

//recv
vector<struct DATA> pData_recv;

recv(mySocket, (char*)&pData_recv, sizeof(DATA);

char name[32];

strcpy(name, pData_recv[0].name);

printf("name: %s", name);

but it does not get all the data, whats wrong? thanks.

Share this post


Link to post
Share on other sites
Advertisement
The vector object itself just contains some pointers and counts. That's not the data you want to send.

What you want to send is the data, pre-fixed with a count of how many items there are. If you're on TCP, this can be done without copying:


count = myVector.size();
count_nw = htonl( count );
send( sock, &count_nw, sizeof(count_nw), 0 );
send( sock, &myVector[0], count*sizeof(myVector[0]), 0 );


On the receiving end, you'd get the count, and resize the receiving array:


recv( sock, &count_nw, sizeof(count_nw), 0 );
count = ntohl( count_nw );
myVector.resize( count );
recv( sock, &myVector[0], cont*sizeof(myVector[0]), 0 );

Share this post


Link to post
Share on other sites
if you want your game to be able to be cross platform (i believe Mac has a different endianess then PC), then you must do this. check the forum FAQ, im sure its mentioned there.

Share this post


Link to post
Share on other sites
do i only have to do this for packet parameters like size ip address or for the content of the buffers as well?

Share this post


Link to post
Share on other sites
IIRC, you must do it for all data. you should convert all data that is > 1 byte to network byte order before sending, and to host byte order when receiving. like i said, i believe this is only if you want Mac and PC to communicate, although, its probably good to do just so that if the chance for porting to Mac came up it would be one less thing to do.

Share this post


Link to post
Share on other sites
In general, you have to do it for the IP address and port number. However, the IP address you get back from gethostbyname() is already translated for you.

The size you pass to the library is used by the library, not put in the packet, so it should be in host order.

Data you put in the packet doesn't HAVE to be network byte order, but it's a good habit to get into; it makes your code more portable, and prevents grief later. Sort of like commenting your own code: you may get away with not doing it NOW...

Share this post


Link to post
Share on other sites
i guess ill just add it even if it means some overhead but on modern computers it shouldn t be a problem i hope ^^

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!