I'm writing a small networked game (pong). currently i'm working on sending my data. this works up to a point, i can send my data by just giving the send function my ball class, i then would revive the data as the same struct and the info gets accost just fine.
but i wish to extend it by adding a chat and some basic login stuff, so i need to make a default packet and int that packet i would have a bit array with the data to send and other parameters that tell how big and what kind of data to expect.
so i tried this but the data is not correctly recieved
typedefenum commands
{
cmdConnect = 1,
cmdConnect_OK = 2,
cmdLOGIN = 10,
cmdERROR = 11,
cmdLOGIN_OK = 12,
cmdREADY = 13,
cmdBEGIN_GAME = 14,
cmdGAME_END = 15
};
struct Packet
{
commands commmand;
int size;
char *Data;
//BALL Data;
};
void Server::Receive ()
{
Packet packet;
recvfrom(Socket, (char*)&packet, sizeof(Packet), 0, (sockaddr*)&RemoteAddress, &SizeInt);
int test = sizeof(packet.Data); //the size is not the same
BALL *ball = (BALL*)packet.Data;
};
so i figure i know why its not working, it because char *Data is a pointer and not an actual array but i just don't understand how to correctly fix this.
Packet packet;
char buffer[1000];
memset(buffer,0,999);
packet.size = recvfrom(Socket, buffer, sizeof(buffer), 0, (sockaddr*)&RemoteAddress, &SizeInt);
packet.commmand = (Command)buffer[0]; //(from 0 to 3 are the pits alocated for this int)
char chararray[sizeof(BALL)]; // i need a way to figure out how to allocate this dinamecley
// the size of ball and login info veries and needs to be alocated dynamecley
// this however is not alowed
//char chararray[packet.size - 4]; is not alowd (-4 is to remove the command bits that we dont use)
memset(chararray,0,8); //clear the memory to 0
int ccount = 0;
for (int i = 4; i < packet.size; i++) //(from 4 to the end is allocated for the data (consits of 2x 4 chars for int))
{
chararray[ccount++] = buffer[i];
}
char *test = (char*)&chararray; // this is not neded
BALL* bal = (BALL*)&chararray; //(BALL*)test; //acsessing the memory though a pointer and alocating it a ball.
Packet packet;
char buffer[1000];
memset(buffer,0,999);
packet.size = recvfrom(Socket, buffer, sizeof(buffer), 0, (sockaddr*)&RemoteAddress, &SizeInt);
packet.commmand = (Command)buffer[0]; //(from 0 to 3 are the pits alocated for this int)
char* chararray = newchar [packet.size - 4];
//char chararray[sizeof(BALL)]; // i need a way to figure out how to allocate this dinamecley
// the size of ball and login info veries and needs to be alocated dynamecley
// this however is not alowed
//char chararray[packet.size - 4]; is not alowd (-4 is to remove the command bits that we dont use)
memset(chararray,0,8); //clear the memory to 0
int ccount = 0;
for (int i = 4; i < packet.size; i++) //(from 4 to the end is allocated for the data (consits of 2x 4 chars for int))
{
chararray[ccount++] = buffer[i];
}
//char *test = (char*)&chararray; // this is not neded
//BALL* bal = (BALL*)chararray; //(BALL*)test; //acsessing the memory though a pointer and alocating it a ball.
BALL balltest; // create origional object
memcpy(&balltest, chararray, sizeof(BALL)); //copy the memory to the new ball object
delete [] chararray; // delete the temproray memeory data
send/receiving data via socket is mostly the same as reading/writing from/to a file.
you cannot use a structure that contains pointer whethe direct or indirect.
if you don't have that much data to transmit you may use a stringstream to read/write the data transmitted.