一、问题描述
在RK3128 设备上(4 核 arm7 32位),定义如下结构体, 然后把结构体变量发送到华为服务器上,64位处理器(x86_64),传到服务器的数据异常!
二、RK3128 设备 上的socket 客户端 程序
1、结构体类型定义 和 变量定义:
typedef struct _dev_all_cpu_freq
{
time_t u32CurTime;
unsigned int u32CPU0Freq;
unsigned int u32CPU1Freq;
unsigned int u32CPU2Freq;
unsigned int u32CPU3Freq;
} S_ALL_CPU_FREQ;
S_ALL_CPU_FREQ mAllCPUFreq;
2、客户端发送数据:
mAllCPUFreq.u32CurTime = time(NULL);
mAllCPUFreq.u32CPU0Freq = read_cpuinfo_cur_freq_file(CPU0);
mAllCPUFreq.u32CPU1Freq = read_cpuinfo_cur_freq_file(CPU1);
mAllCPUFreq.u32CPU2Freq = read_cpuinfo_cur_freq_file(CPU2);
mAllCPUFreq.u32CPU3Freq = read_cpuinfo_cur_freq_file(CPU3);
ret = write(server_socket_hander,(void*)&mAllCPUFreq,sizeof(mAllCPUFreq));
3、客户端数据打印:
Read CPU Cur Freq's Time : Thu Sep 24 16:44:35 2020
CPU0 cur freq = 1200000
CPU1 cur freq = 1200000
CPU2 cur freq = 1200000
CPU3 cur freq = 1200000
三、华为服务器上,socket 服务器端
1、结构体类型定义 和 变量定义:
typedef struct _dev_all_cpu_freq
{
time_t u32CurTime;
unsigned int u32CPU0Freq;
unsigned int u32CPU1Freq;
unsigned int u32CPU2Freq;
unsigned int u32CPU3Freq;
} S_ALL_CPU_FREQ;
S_ALL_CPU_FREQ mAllCPUFreq;
2、服务器端接收数据:
ret = read(connect_socket_fd,(void*)&mAllCPUFreq,sizeof(mAllCPUFreq));
3、服务器端打印
Read CPU Cur Freq's ctime : Thu May 19 05:56:05 163324535
Read CPU Cur Freq's Time : 163324535/5/19_5:56:5
CPU0 cur freq = 1200000
CPU1 cur freq = 1200000
CPU2 cur freq = 1200000
CPU3 cur freq = 0
四、问题分析
RK3128 设备 上的socket 客户端 程序上,time_t u32CurTime; 这个变量占4个字节
printf("sizeof(time_t) = %d ",sizeof(time_t));
sizeof(time_t) = 4
华为服务器上,socket 服务器端 ,time_t u32CurTime; 这个变量占8个字节
printf("sizeof(time_t) = %d ",sizeof(time_t));
sizeof(time_t) = 8
所以,time_t 这种根据设备来决定位数的类型慎重用!!!
五、解决方案:
u32CurTime 数据类型改成 long long
typedef struct _dev_all_cpu_freq
{
uint32_t u32CurTime; // 占4个字节
unsigned int u32CPU0Freq;
unsigned int u32CPU1Freq;
unsigned int u32CPU2Freq;
unsigned int u32CPU3Freq;
} S_ALL_CPU_FREQ;