Advertisement

Casting structures

Started by May 12, 2003 04:39 AM
4 comments, last by omega_001 21 years, 8 months ago
Can anyone help me with the following problem: I'm trying to send a structure across a TCP connection using Winsock by casting it as a char* so the method send() doesn't throw a fit. However whenever I reassemble the structure back at the other end of the network the buffer is always empty although it says it received 4 bytes of information (this may of been a good sign accept the structure is over 100 bytes in size). Below is my server code for sending the data: SENSOR_DATA data; //structure to send char* buffer; buffer=(char*)malloc(sizeof(char)); buffer=&data send(masterSocket,buffer,sizeof(data),0); Below is my client code for reassembling the data: SENSOR_DATA *data; char *buffer; buffer=(char*)malloc(sizeof(char)); receive(masterSocket,buffer,sizeof(buffer),0); data=(SENSOR_DATA*)buffer; Can anyone help? The server code always returns the correct number of bytes that should of been sent but I'm confused as to why their not received on the other end. I know the fact that the struture contains serveral pointers may be the issue however some of the data must of been sent as not all of its represented by a pointer e.g. char text[512]. Thanks in advance, K. [edited by - omega_001 on May 12, 2003 6:27:25 AM]
quote:
Original post by omega_001
Can anyone help me with the following problem:

...

SENSOR_DATA data; //structure to send
char* buffer;

buffer=(char*)malloc(sizeof(char));
buffer=&data

send(masterSocket,buffer,sizeof(data),0);




Ok, your problem is simple.. what is buffer defined as ? a pointer to a character !
and how big it a pointer ? 4 bytes ? .. yes
so what are you sending... a pointer to the buffer on the other computer :/ hehe

so what should it be you say ?

well for starters you do this..
>>buffer=(char*)malloc(sizeof(char));

which is allocating 1 byte to the buffer pointer.. then you go and so this..

buffer=&data
which meens you just overwrote the old pointer to the byte you allocated and can now never free (not that u wanted to allocate it in the 1st place) so you have memory leaks already

ok getting to the point you need to do the following:
scrap all your code and do this..

send(masterSocket,(char *)&data,sizeof(data),0);


how simple ?

quote:


Below is my client code for reassembling the data:

SENSOR_DATA *data;
char *buffer;

buffer=(char*)malloc(sizeof(char));

receive(masterSocket,buffer,sizeof(buffer),0);

data=(SENSOR_DATA*)buffer;

Can anyone help? The server code always returns the correct number of bytes that should of been sent but I''m confused as to why their not received on the other end.

I know the fact that the struture contains serveral pointers may be the issue however some of the data must of been sent as not all of its represented by a pointer e.g. char text[512].

Thanks in advance, K.



[edited by - omega_001 on May 12, 2003 6:27:25 AM]



change it to this..

SENSOR_DATA *data;

receive(masterSocket,data,sizeof(data),0);






~ Tim
~ Tim
Advertisement
quote:
Original post by wrathgame
change it to this..

SENSOR_DATA *data;

receive(masterSocket,data,sizeof(data),0);

Gah.

When you reply, try to be helpful, eh? Your way corrupts memory at an uninitialized pointer.

SENSOR_DATA data;
receive( masterSocket, &data, sizeof(data), 0 );

Actually, wrathgame is almost correct.

The first thing is that you don't need to malloc on the send. Simply change:
quote:

buffer=(char*)malloc(sizeof(char));
buffer=&data


to
buffer = (char *)&data 


Next.. on the receive side, change the following code:
quote:

buffer=(char*)malloc(sizeof(char));

receive(masterSocket,buffer,sizeof(buffer),0);


to
buffer=(char*)malloc(sizeof(SENSOR_DATA));receive(masterSocket,buffer,sizeof(SENSOR_DATA),0); 


If I typed in everything correctly, this should fix your problem.

Hope that helps.

There will be other issues with your code such as the data you are receiving may not be SENSOR_DATA, but you can figure that out on your own.






Dino M. Gambone
Good judgement is gained through experience. Experience, however, is gained through bad judgement.



[edited by - dino on May 15, 2003 4:01:50 PM]

Dino M. Gambone
Good judgment is gained through experience. Experience, however, is gained through bad judgment.

Currently working on Rise of Praxis MUD: http://www.riseofpraxis.net/

Doh! Maybe before I reply I should finish reading the additional posts. Sorry for the repetition.

Dino M. Gambone
Good judgment is gained through experience. Experience, however, is gained through bad judgment.

Currently working on Rise of Praxis MUD: http://www.riseofpraxis.net/

Thanks guys, that helped loads.

p.s. on a further note I hate pointers.

This topic is closed to new replies.

Advertisement