I have some code that sends UDP sockets to a server. Currently I have a separate server code that I run locally which reads anything that's sent to it and writes back exactly what it receives.
The next step I need to make is to send and receive structs. I can send the struct fine, but when I receive it back from the server it's mashed up. Here is my code:
typedef struct {
char first_part[4];
char second_part;
char third_part[2];
} Cloud;
Then in main
:
char reply[BUFLEN], message[BUFLEN];
Cloud data;
strcpy(data.first_part, "test");
data.second_part = 'a';
strcpy(data.third_part, "hi");
printf("Size:%d\n", sizeof(data));
//This part seems to work---
char* package;
package = (unsigned char*)malloc(sizeof(data));
memcpy(package, &data, sizeof(data));
printf("Size:%d\n", strlen(package));
strcpy(message, package);
udp_send_receive(message,reply);
//---So the message is sent, and the string received by the server is correct.
memcpy(package, message, strlen(message));
printf("Package: %s\n",package); //-This is also correct
memcpy(&data, package, sizeof(data));
printf(data.first_part); //--This is incorrect
I would be really grateful if someone could explain what is going wrong here. I'm a little inexperienced with this sort of thing and I have been tasked with building a UDP server that communicates with another server, where the data is transferred in specific structures.