转换数据从TCP套接字在python



我有一个数据结构,我正在转换为uint8_t并通过tcp套接字连接发送它。对于服务器端,我有一个python。如何转换数据和显示结构信息。

//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);

请举个例子。

谢谢

使用https://www.digitalocean.com/community/tutorials/python-socket-programming-server-client socket_server.py代码作为服务器应用程序

你根本不能做std::memcpy(data.data(), reinterpret_cast<void*>(&student),size)

&student不是POD类。具体来说,在内存中,string name;成员将是一个小结构体,比如16字节,带有指向实际内容的指针。

使用类似Protobuff或任何其他股票序列化。这是唯一正确的方法。

最新更新