Editor loading... little knowledge: byte ordering is rarely concerned, because it is rarely used. What is the byte order? Let's look at an example. Suppose there is a word-type variable whose value is 0x7788. How does it store it in the memory? Memory: Low address, high address, 0x77 0x88 high address, low address, 0x77 0x88 Figure 3.7, in fact, for different CPUs and different operating systems, the two bytes in Figure 3.7 are all possible. If, as shown on the left side of Figure 3.7, the high byte is in front and the low byte is in the back, the byte sequence is called Big-Endian. If the low byte is in front of the lower byte as shown on the right side of Figure 3.7, after the high byte, the byte sequence is called Little-Endian. Table 3.1 Common CPU and operating system bytes ordered CPU operating systems x86 (Intel, AMD, etc) all little-Endian DEC Alpha all little-Endian HP-PA nt little-Endian HP-PA UNIX big-Endian Sun iSCSI all big-Endian MIPS nt little-Endian mips unix big-Endian PowerPC nt little-Endian- endian PowerPC non-nt big-Endian RS/6000 UNIX big-Endian Motorola m68k all big-Endian generally, we don't need to worry about the byte sequence unless cross-platform communication and resource sharing are involved, such as the network programming described in this chapter (the network transmission protocol TCP/IP uses big-Endian ). What if we want to transmit and exchange data between machines in different byte sequences? (Different machines may have different understandings of the same data. It is not the same as the original intention !) There are two methods: one is to convert all data to the original file for transmission, the other is that both parties transmit data in the byte order of one party (in this case, there is a problem of mutual conversion between different byte sequences ). The second method is often used in socket programming. The entire transmission process is as follows: the sender converts the local data to the bytes of the network (call the API function htonl or htons), and then sends the data. After receiving the network data, the Receiver, first, the bytes of the data conversion cost machine (call the API function ntohl or ntohs), and then perform other operations-so that the "meeting spirit" can be correctly transmitted between the communication parties! Several API functions used in this process: ntohl, htonl, ntohs, and htons. The names are similar and difficult to distinguish. But if we know their origins, the problem does not exist: N is network, the meaning of the network; H is host, the meaning of the local host. Ntohl refers to converting 32-bit u_long data from the network byte sequence to the machine byte sequence (htonl's byte sequence conversion process is opposite to ntohl); ntohs, this means that the 16-bit u_short type data is converted from the network byte order to the machine byte order (htons's byte sequence conversion process is opposite to ntohs ). Finally, there is a small question: how can we know the byte sequence of the Local Machine? There is a simple method: bool islittleendian (void) {word wvalue = 0x5678; Return (* (byte *) & wvalue) = 0x78 );} in addition, both the tcpserver program and the tcpclient program use a tool class unetwork for implementation. This class implements two static member functions: gethostinfo and dumpsocketerror. The former is used to obtain the IP address and machine name of the local host, and the latter is used to track socket errors during program debugging. Especially the dumpsocketerror function is very useful. Because debugging of the socket program is generally troublesome, The dumpsocketerror function can convert the integer socket error code into a string description that is easy to understand and output, which is very convenient! However, the question is, why does htons need to be used to convert the byte sequence when creating a socket or bind, but does not need to be converted when the send function sends data in the future? I want to see if the underlying protocol is converted, but I want to change it to "handshake" during creation. After the "Consensus" is reached, the Protocol (underlying Program) to complete this work
Original article: http://blog.csdn.net/xiaoxiongli/article/details/1955877