I want to send/receive structs containing float and int variables in client/server socket programs written in C++.
Earlier, the code was written in C, so I was simply sending them as follows:
//structure definition
struct {
float a;
float b;
float c;
float d[2];
}myStruct_;
//code to send struct
int slen = sizeof(client_sock)
if(recvfrom(sockFD, &myStruct_, sizeof(myStruct_), 0 ,(struct sockaddr *)&client_sock, &slen)<0) {
printf("Failure while receiving data %d" , WSAGetLastError());
getchar();
exit(1);
}
But now in C++, this is giving me this error message:
error: cannot convert '(anonymous struct)*' to 'char *' for argument '2' to 'int recvfrom(SOCKET, char*, int, int, sockaddr*, int*)'
I tried to look for this error, and found out that I have to serialize the struct before sending, and later deserialize the same to get the exact structure. Could you suggest/or provide an example how to serialize and de-serialize it?
struct
tochar*
and copy the receive buffer into that. – Galik