You are allocating only 1 byte for the recv()
buffer, but you are trying to read sizeof(ServerPacket_SyncGame)
number of bytes into it. You need to change this:
char* SP_SG_RCVBUFF = new char; //new buffer for recv
To this:
char* SP_SG_RCVBUFF = new char[sizeof(ServerPacket_SyncGame)];
Same thing with your for
loop:
for(;;)
{
//char* buffer = new char;
char* buffer = new char[sizeof(ServerPacket_SyncGame)];
//char* temp = new char;
char* temp = new char[sizeof(ServerPacket_SyncGame)];
...
};
I would suggest you clean up your code:
ServerPacket_SyncGame* SP_SG = new ServerPacket_SyncGame; //creates packet pointer
for(int i = 0; i < 8; i++) //assigns the eight playerdata structs in the packet array
{
SP_SG->data[i].playerID = i;
SP_SG->data[i].health = rand() % 30;
SP_SG->data[i].moveflags = 'D';
SP_SG->data[i].pos.x = rand() % 1000;
SP_SG->data[i].pos.y = rand() % 1000;
SP_SG->data[i].rotation = rand() % 360;
}
SP_SG->packetID = 5; //assigns the packet id
...
// don't forget to do error handling on this, and pay attention to the
// return value so you know if you actually sent the entire struct or not...
send(Socket, (char*)SP_SG, sizeof(ServerPacket_SyncGame), 0);
delete SP_SG;
SP_SG = new ServerPacket_SyncGame;
// don't forget to do error handling on this, and pay attention to the
// return value so you know if you actually received the entire struct or not...
recv(Socket, (char*)SP_SG, sizeof(ServerPacket_SyncGame), 0); //recv new buffer
...
delete SP_SG;
ServerPacket_SyncGame buffer;
for(;;)
{
// don't forget to do error handling on this, and pay attention to the
// return value so you know if you actually received the entire struct or not...
int size = recv(Socket, &buffer, sizeof(ServerPacket_SyncGame), 0);
if (size > 0)
{
// don't forget to do error handling on this, and pay attention to the
// return value so you know if you actually sent the entire struct or not...
send(Socket, (char*)InterpretInfo(&buffer), size, 0);
}
};