Reputation: 65
I have a data struct that I am converting to uint8_t and sending it over a tcp socket connection. For the server side I have a python. How do I convert the data and display the struct information.
//data struct
typedef struct {
int enable;
string name;
int numbers[5];
float counter;
}Student;
//convert data to uint8 and send over tcp socket
uint32_t size = sizeof(student);
std:vector<std::uint8_t> data(size);
std::memcpy(data.data(), reinterpret_cast<void*>(&student),size)
sendDataOverTCPSocket(data);
Appreciate an example on how to do this.
Thank you
Used this https://www.digitalocean.com/community/tutorials/python-socket-programming-server-client socket_server.py code as the server application
Upvotes: 0
Views: 178
Reputation: 11410
You simply cannot do std::memcpy(data.data(), reinterpret_cast<void*>(&student),size)
&student
is not a POD class. Specifically, in memory, the string name;
member will be a small struct, say 16 bytes, with pointers to the actual content.
Use something like Protobuff or any other stock serialization. This is the only sane way.
Upvotes: 1