I am developing a client / server program and my client should send messages to the server.
Example message structure C :
struct Registration { char multicastGroup[24]; pid_t clientPid; };
client code snippet for serializing structure
struct Registration regn ; regn.clientPid = getpid(); strcpy(regn.multicastGroup, "226.1.1.1"); printf("PID:%d\n", regn.clientPid); printf("MG:%s\n", regn.multicastGroup); printf("Size:%d\n", sizeof(regn)); //Size is 28 data = (unsigned char*)malloc(sizeof(regn)); memcpy(data, ®n, sizeof(regn)); printf("Size:%d\n", sizeof(data)); //Size is 4.
Server code for de-serializing data
if(recvfrom(sd, recvBuf, recvBufSize, 0, (struct sockaddr*)&clientAddr, &len) < 0) { printf("Error receiving message from client\n"); } else { printf("Message received:%s\n", recvBuf); printf("Size :%d\n", strlen(recvBuf)); memcpy(®n, recvBuf, sizeof(regn)); printf("PID:%d\n", regn.clientPid); printf("MG:%s\n", regn.multicastGroup); }
After copying the structure to unsigned char * size of the array will be 4. Why are the data not completely copied to the array?
The server cannot restore the structure from the char array.
Please let me know what I am doing wrong.
c serialization client-server
cppcoder
source share