Here is my send function:
bytecount = sizeof Player; bytecount = htonl(bytecount); send(socketinfo, (char*)&bytecount, sizeof(bytecount), 0); if (socketinfo == SOCKET_ERROR) { send(socketinfo, (char*)&bytecount, sizeof(bytecount), 0); } con << bytecount << "\n"; bytecount = ntohl(bytecount); Player* ptr = &Send send(socketinfo, (char *)ptr, bytecount ,0); if (socketinfo == SOCKET_ERROR) { send(socketinfo, (char*)ptr, bytecount ,0); }
and here is my recieve function:
Player X; Player *ptr = &X int bytecount = sizeof(Player); bytecount = htonl(bytecount); //recv(Clients[a].Client,buffer,1,0); recv(Clients[a].Client, (char*) &bytecount, sizeof(bytecount), 0); if (Clients[a].Client == SOCKET_ERROR) { recv(Clients[a].Client, (char*) &bytecount, sizeof(bytecount), 0); } recv(Clients[a].Client, (char*)ptr, bytecount, 0); if (Clients[a].Client == SOCKET_ERROR) { recv(Clients[a].Client, (char*)ptr, bytecount, 0); } con.Goto(12,0); if(ptr->fight == 204) { con.Goto(5,0); con << "Client Disconnected!\n"; Disconnect(a); } else { con << "Message Recieved!\n"; con << "Name : " << ptr->name << "\n"; con << "X : " << ptr->x << "\n"; con << "Y : " << ptr->y << "\n"; con << "Fight : " << ptr->fight << "\n"; }
Now this way works great for my machine and only my machine due to the pointer being caught since it points to a valid memory location on my computer, for anyone else that connects and sends the struct pointer it just shows bad values on the servers end. I''ve been told I need to pack the entire thing into a character buffer and then some how parse the buffer based on the byte size of each variable and then display those.
Can anyone show me the proper way to send a struct or any other datatype within a character buffer using send and recv?