unp里讲到不同平台传输二进制会因为字节序或者位数不同的问题产生问题,那应该怎么解决这个问题呢? 新手。。。求大神提供点这方面的资料。。。 还有为什么传字符串就不会有问题呢? 不会有问题的是一个字节的字符吧。。比如ASCII码,一个字符只占一个字节那不会有字节序问题,那要传多字节字符呢?比如传输中文 大端小端存储的也不一样吧? 难道不会有问题?
阿神2017-04-17 13:19:01
Just unify little endian. Although the network byte order is big segment. However, the most popular device at present, that is, x86, is little endian, and those big endian machines also support little endian. Like Both arm and powerpc support little endian. So don’t worry about it, just use little endian.
PHPz2017-04-17 13:19:01
Byte order is generally for numbers. The encoding in a string defines the order of bytes, so using the same string encoding determines the same byte order. The endianness of numbers is a historical issue and has not yet been unified. It is also very simple to deal with the endianness of numbers. Do not use the system's default digital writing or reading method to read data (because this method uses Default byte order), instead, you split the number into big-endian or little-endian bytes through the program, and manually combine the big-endian or little-endian data when reading, thus ensuring a unified character for reading and writing. Sequence.
阿神2017-04-17 13:19:01
Specify Big-Endian or Little-Endian when writing, and use the same order when reading.
PHP中文网2017-04-17 13:19:01
The essential problem of byte order is the problem of type values of two bytes or more. When the network transmits data, it is a binary stream. What the data looks like when it is sent is what it is when it is received. If the machine size at both ends of the socket is The end of the struct is the same, or there is no data of more than two bytes (just like strings are data composed of a single byte, but if the struct contains multi-byte data, it needs to be converted), network byte order is useless , only the machines at both ends of the socket are different, their understanding of binary data such as 0x1234 is different, so you need to call the hton* series of functions to perform corresponding conversions to get the correct data.