//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);
Send Vector Lists.
Hi, I just wounder if its possible to send vector Lists and recv them?
Can I do something like this?
but it does not get all the data, whats wrong?
thanks.
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:
On the receiving end, you'd get the count, and resize the receiving array:
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 );
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.
do i only have to do this for packet parameters like size ip address or for the content of the buffers as well?
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.
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...
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...
i guess ill just add it even if it means some overhead but on modern computers it shouldn t be a problem i hope ^^
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement