WinSock Sending data

Hi,

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.

like this
1
2
3
4
  BALL Data;

  sendto(Socket, (char*)&Data, sizeof(Data), 0, (sockaddr*)&RemoteAddress,
           sizeof(sockaddr));


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
typedef enum 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.

i appreciate any help that can be given.
Last edited on
Ok so i awoke this morning with some inspiration, but i do have one more question

how would i dynamicaly allocate an array of chars, my current code below shows how i fixed the problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
	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. 
if you want a dynamical allocation of memory why don't u switch to link list.
Prashant Gupta PG: thanks for the tip(at the same time i was reading up on the same thing :} ), i managed to get what i needed like this.

i hope i did it right, of not i appreciate any help


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
	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 = new char [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.

Topic archived. No new replies allowed.