c++ - TCP网络编程一般都是怎么封包解包的? 怎么才能解决不同平台的字节序和不同整数大小的问题?
天蓬老师
天蓬老师 2017-04-17 13:17:01
0
4
765

unp里讲到不同平台传输二进制会因为字节序或者位数不同的问题产生问题,那应该怎么解决这个问题呢? 新手。。。求大神提供点这方面的资料。。。 还有为什么传字符串就不会有问题呢? 不会有问题的是一个字节的字符吧。。比如ASCII码,一个字符只占一个字节那不会有字节序问题,那要传多字节字符呢?比如传输中文 大端小端存储的也不一样吧? 难道不会有问题?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(4)
阿神

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.

PHPzhong

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.

阿神

Specify Big-Endian or Little-Endian when writing, and use the same order when reading.

洪涛

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template