1

I am currently working in making a game with online, a simple game, my first time with SDLNet. I was thinking on each client sending structs with the position to the server and the server handle sending it to the others clients. The problem comes when tryng to send structs that contains vectors. If the structs only contains ints there isn't any segfault. I will leave some of the code:

struct STRUCT_TO_SEND {
    std::vector <int> posXs;
    std::vector <int> posYs;
};
int buffer_zise = 200;
char buffer[buffer_zise];

//From the client side
STRUCT_TO_SEND test; 
test.posXs.push_back(2);
test.posYs.push_back(2);
memcpy(buffer, &test, sizeof(test));
SDLNet_TCP_Send( client, buffer, buffer_zise );

//From the server side
SDLNet_TCP_Recv(clientSocket[i], buffer, buffer_zise);
STRUCTURA_TO_SEND tmp;
memcpy(&tmp, buffer, sizeof(STRUCT_TO_SEND)); //<--Getting segfault
std::cout << tmp.posXs[0] << " " << tmp.posYs[0] << std::endl; //<--Getting segfault
  • what's `STRUCTURA_SNAKE_PRUEBA`? – Martin G Feb 22 '16 at 18:18
  • `memcpy(buffer, &test, sizeof(test));` -- This line is totally invalid. You don't send or copy non-POD types using `memcpy`. If you want proof, stick in 10,000 elements into that vector and then see what `sizeof(test)` will be. You will see that `sizeof(test)` is still some very low number (40, 50, for example) -- it isn't 10,000+. See this: http://stackoverflow.com/questions/234724/is-it-possible-to-serialize-and-deserialize-a-class-in-c Bottom line is that you want to send the **data** that is in the vectors, not the vectors. – PaulMcKenzie Feb 22 '16 at 18:20
  • @Martin sorry was something about my code, already edited. – Mauro Schiavi Feb 22 '16 at 21:32
  • @PaulMcKenzie thanks for the answer, i will look it out. – Mauro Schiavi Feb 22 '16 at 21:33

0 Answers0