send stl vector by a socket

Joined
Dec 7, 2008
Messages
1
Reaction score
0
Hi everybody!

I'm a relative newbie in programming, and i have to make an client/server application. So... this application comunicate by a couple of sockets. In the implementation, i found me in a situation that i need to send an stl vector (because in the server size, this array is extremaly variable, and that is why i declare it a stl vector) to the cliente. But i have try some codes and always end in a segmentation fault.

So... is there a way that i can send an stl vector??

Well... i will post a demonstration of my problem:
Code:
client:
   vector<int> a;
   a.push_back(2);
   a.push_back(10);
   a.push_back(11);
   
   result = write( sockfd, &a, sizeof(a) );
   cout << result << "\n";

Code:
server:
   vector<int> *a = new vector<int>;
   
   result = read( client_sockfd, &a, sizeof(*a) );
   cout << result << "\n";
   cout << a->back() << "\n";

i already try many variations of these codes (made all vector pointers, cast to void* and etc), and none seen to work.

the funny thing is that value of result is 12 (in both sides), so... the length is ok. other thing is when i comment the line 'cout << a->back()', the program doesn't abort.

well, thanks very much to everyone
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top