简体   繁体   English

将结构类型的变量发送到套接字

[英]sending variable of type struct into socket

For a study project, can I send a variable of type struct from server to client that had information of an employee(ID,salary etc), actually I am not so much used to of C so I am not much clear about this concept.对于学习项目,我可以从服务器向客户端发送一个 struct 类型的变量,其中包含员工的信息(ID、工资等),实际上我不太习惯 C,所以我对这个概念不太清楚。 or give me any idea that in which form i can send this information on socket.或者让我知道我可以以哪种形式在套接字上发送此信息。

While the struct contents can be transmitted, this is likely to cause problems.虽然可以传输结构内容,但这很可能会导致问题。

Struct alignment varies between compilers, target CPUs, and version of CPU.结构对齐因编译器、目标 CPU 和 CPU 版本而异。 Also, the size of each structure element requires care so that it is a consistent size across architectures.此外,每个结构元素的大小都需要小心,以便跨架构保持一致的大小。

struct s {
     int          a;
     short        b;
     long         c;
     long double  d;
};

While most engineers are aware that int could be anywhere from 16 to 128 bits, depending on the target architecture, the other fields also might have sizes which are different from target to target.虽然大多数工程师都知道int可以是 16 到 128 位的任何位置,具体取决于目标架构,其他字段的大小也可能因目标而异。 long double might be equivalent to float on the most primitive embedded CPUs and could be 64, 80, or 128 bits on mainstream architectures. long double可能是等同于float在最原始的嵌入式CPU,并且可以是对主流结构64,80,或128比特。

To resolve these problems, a full spectrum of solutions have evolved: from full control of both ends of a communication link to converting the data to a streaming format like XML.为了解决这些问题,出现了一系列解决方案:从完全控制通信链路的两端到将数据转换为 XML 等流格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM