Skip to main content
gamedev.net
🔒 Locked

Send Vector Lists.

Started by X5-Programmer Jan 6, 2005 at 12:00 PM 7 replies 1k views
Original Post
X5-Programmer
X5-Programmer
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 ]-
hplus0603
hplus0603
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 };
Basiror
Basiror
hplus why do you convert the count to tcp networkbyte order?
http://www.8ung.at/basiror/theironcross.html
graveyard filla
graveyard filla
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
Basiror
Basiror
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
graveyard filla
graveyard filla
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
hplus0603
hplus0603
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 };
Basiror
Basiror
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.