Send Vector Lists.

Started by
7 comments, last by Basiror 19 years, 3 months ago
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.
-[ thx ]-
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 );

enum Bool { True, False, FileNotFound };
Thanks hplus0603.

Got it now. =)
-[ thx ]-
hplus why do you convert the count to tcp networkbyte order?
http://www.8ung.at/basiror/theironcross.html
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.
FTA, my 2D futuristic action MMORPG
do i only have to do this for packet parameters like size ip address or for the content of the buffers as well?
http://www.8ung.at/basiror/theironcross.html
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.
FTA, my 2D futuristic action MMORPG
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...
enum Bool { True, False, FileNotFound };
i guess ill just add it even if it means some overhead but on modern computers it shouldn t be a problem i hope ^^
http://www.8ung.at/basiror/theironcross.html

This topic is closed to new replies.

Advertisement