简体   繁体   English

是否可以使用SSL_write和SSL_read发送结构?

[英]Is it possible to send a struct with SSL_write and SSL_read?

Is it possible to send and recieve a structure with SSL_write and SSL_read? 是否可以使用SSL_write和SSL_read发送和接收结构?

Client 客户

typedef struct{
      unsigned int userid;
      unsigned int name;        
} sendInfo;

  sendInfo infofile;



SSL_write(ssl, infofile, sizeof(struct infofile)); 

Server 服务器

bytes = SSL_read(ssl, struct(infofile), sizeof(struct infofile) );

Is there other methods to send a structure? 还有其他发送结构的方法吗?

Cheers. 干杯。

You should not do this in general. 通常,您不应该这样做。 C data structures do not have a mandatory, universal memory layout that's the same on all architectures and platforms. C数据结构没有在所有体系结构和平台上都相同的强制性通用内存布局。 When you serialize data, you always have to break it down into individual parts (integers and byte sequences), and moreover you will have to specify the size and ordering (endianness) of all multibyte fields like integers. 序列化数据时,始终必须将其分解为各个部分(整数和字节序列),而且,您还必须指定所有多字节字段(例如整数)的大小和顺序(字节序)。

Imagine that you find a block of bytes on the street. 想象一下,您在大街上发现了一个字节块。 How would you know what it means? 您怎么知道这意味着什么? When you serialize, you have to publish a specification of the format, something which you could apply to your found bytes and say, "this is the first integer", "this is a sequence of bytes, counted by the previously read integer", etc. With this information, the corresponding deserializing code can rebuild the internal data structure. 序列化时,您必须发布格式的规范,可以将其应用于所找到的字节,并说“这是第一个整数”,“这是字节序列,由先前读取的整数计算”,有了这些信息,相应的反序列化代码就可以重建内部数据结构。

A typical serialization field would be something like "unsigned 32-bit integer, little-endian". 典型的序列化字段将类似于“无符号32位整数,little-endian”。 That means you have to write out bytes n & 0xFF , (n >> 8) & 0xFF , (n >> 16) 0xFF , (n >> 24) & 0xFF , and you read back in buf[0] + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24) . 这意味着您必须写出字节n & 0xFF(n >> 8) & 0xFF(n >> 16) 0xFF(n >> 24) & 0xFF ,然后以buf[0] + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24) Note that you'll never have to know anything about your platform, but only about the wire format. 请注意,您将不必了解平台,而只需了解有线格式。

除了您可能是指&infofile

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

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