我通过函数SENDTO通过套接字(c++)发送结构,并通过RECVFORM接收,但总是得到一些垃圾值



嘿,我正在开发一个工具NCTUns。这里我通过sendto函数发送结构。

我的结构-

struct agentClientReportStatus{ 
u_int32_t      type;
u_int32_t      nid;
double         timeStamp;
int            flag;
u_int32_t      moreMsgFollowing;
double         x;
double         y;
double         x1;
double         y1;
double         acceleration;
double         speed;
double         direction;
int            TTL;
int           seqNum;
}    

和功能是-

 n = sendto(myUDPsockfd, msg, sizeof(struct agentClientReportStatus), 0, (struct sockaddr *) &cli_addr, len); 
 n = recvfrom(myUDPsockfd, (char *)&msg, sizeof(struct agentClientReportStatus), 0, (struct sockaddr *) &cli_addr, &len);  

在发送部分,当我打印一些值时,它会给出正确的输出。但在接收部分,我得到了垃圾价值。可以在不序列化的情况下发送它吗?如果没有,那么我如何进行序列化?

msg属于-agentClientReportStatus*msg;msg=新agentClientReportStatus;

接收器可以具有

  • 不同的内存对齐
  • 不同的endianes(处理endianes的有用函数有htonlntohl等)
  • 不同尺寸的标准c型

所以答案是肯定的,您应该使用序列化

最新更新