需要传输的数据如下:
float volume = 0.9876;
char send_buff[1024] = {0};
int send_len = 0;
一、发送端数据处理
###错误示例如下###
错误1.
float tmp = 0.0;
tmp = htonl(volume);
memcpy(send_buff, &tmp, sizeof(tmp));
send_len += sizeof(tmp);
错误2.
uint32_t tmp = 0;
tmp = htonl(volume);
memcpy(send_buff, &tmp, sizeof(tmp));
send_len += sizeof(tmp);
###正确示例如下###
uint32_t tmp = 0;
memcpy(&tmp, &volume, sizeof(volume));
tmp = htonl(tmp);
memcpy(send_buff, &tmp, sizeof(tmp));
send_len += sizeof(tmp);
二、接收端数据处理
###错误示例如下###
错误1.
float tmp = 0.0;
memcpy(&tmp, send_buff, sizeof(tmp));
tmp = ntohl(volume);
错误2.
uint32_t tmp = 0;
float volume_recv = 0.0;
memcpy(&tmp, send_buff, sizeof(tmp));
volume_recv = ntohl(tmp);
###正确示例如下###
uint32_t tmp = 0;
float volume_recv = 0.0;
memcpy(&tmp, send_buff, sizeof(tmp));
tmp = ntohl(tmp);
memcpy(&volume_recv, &tmp, sizeof(volume_recv);