In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
In CPU, the storage method of 4-byte integer 1 in memory space is different. A 4-byte integer 1 can be represented in binary as follows:
00000000 00000000 00000000 00000001
Some CPU are stored in memory in the following order, while others are stored in reverse order, as shown below:
00000001 00000000 00000000 00000000
If you do not think about these, there will be achievements in sending and receiving data, because the difference in the order of custody means that there is also a difference in the order of parsing the accepted data.
Large end sequence and small end order
There are two ways for CPU to store data in memory:
Large end order (Big Endian): high-order bytes are stored to low-order addresses (high-order bytes come first).
Small end order (Little Endian): the high-order bytes are registered to the high-order address (the low-order bytes come first).
It is difficult to explain clearly based on the description alone, so let's take a look at an example. Assume that the 4-byte int data 0x12345678 is kept in the address at the beginning of the 0x20 number, and the large-end CPU storage method is shown in the following figure:
Figure 1: large end-order byte representation of integer 0x12345678
For large end order, the highest byte 0x12 is registered to the low address, and the lowest byte 0x78 is registered to the high address. The storage method of the small end sequence is as follows:
Figure 2: small end-order byte representation of integer 0x12345678
The method of keeping and analyzing data in CPU is different (the mainstream Intel series CPU is small end sequence). Data parsing problems will occur in the communication between small end sequence and large end sequence. So before sending the data, you need to convert the data into a consistent pattern-collecting byte order (Network Byte Order). The collection byte order is consistent with the large end order.
Host A first converts the data into a large end sequence and then stops collection and transmission, and host B first converts the data into its own pattern and then parses it.
Collection byte order conversion function
The sockaddr_in constructor is explained in the section "using the bind () and connect () functions", where the collection byte order conversion function is used, as follows:
/ create the sockaddr_in constructor variable struct sockaddr_in serv_addr; memset (& serv_addr, 0, sizeof (serv_addr)); / / fill every byte with 0 serv_addr.sin_family = AF_INET; / / use the IPv4 address serv_addr.sin_addr.s_addr = inet_addr ("127.0.0.1"); / / detailed IP address serv_addr.sin_port = htons (1234); / / end banner
Htons () is used to convert future host byte order into collection byte order, where h represents host (host) byte order, n represents collection (network) byte order, and s represents short,htons is a combination of h, to, n, s, which can be understood as "converting short,htons data from future host byte order to collection byte order".
Rare collection byte conversion functions are:
Htons (): host to network short, which converts short type data from host byte order to collection byte order.
Ntohs (): network to host short, which converts short type data from collection byte order to host byte order.
Htonl (): host to network long, which converts long type data from host byte order to collection byte order.
Ntohl (): network to host long, which converts long type data from collection byte order to host byte order.
On weekdays, in functions with the suffix s, s represents 2 bytes of short, so it is used for end slogan translation; in functions with the suffix of l, l represents 4 bytes of long, so it is used for IP address translation.
An example is given to illustrate the misappropriation process of the above functions:
# include # pragma comment (lib, "ws2_32.lib") int main () {unsigned short host_port = 0x1234, net_port; unsigned long host_addr = 0x12345678, net_addr; net_port = htons (host_port); net_addr = htonl (host_addr); printf ("Host ordered port:% # x\ n", host_port); printf ("Network ordered port:% # x\ n", net_port) Printf ("Host ordered address:% # lx\ n", host_addr); printf ("Network ordered address:% # lx\ n", net_addr); system ("pause"); return 0;}
Operational consequences:
Host ordered port: 0x1234
Network ordered port: 0x3412
Host ordered address: 0x12345678
Network ordered address: 0x78563412
Other requirements clarify that the member that holds the IP address in sockaddr_in is a 32-bit integer, while we are familiar with dotted decimal representation, such as 127.0.0.1, which is a string, so in order to assign IP addresses, you need to convert the string to a 4-byte integer.
The inet_addr () function can do this conversion. Inet_addr () not only converts the string to a 32-bit integer, but also stops collecting byte order conversions. Take a look at the following code:
# include # pragma comment (lib, "ws2_32.lib") int main () {char * addr1 = "1.2.3.4"; char * addr2 = "1.2.3.256"; unsigned long conv_addr = inet_addr (addr1); if (conv_addr = = INADDR_NONE) {puts ("Error occured!");} else {printf ("Network ordered integer addr:% # lx\ n", conv_addr);} conv_addr = inet_addr (addr2) If (conv_addr = = INADDR_NONE) {puts ("Error occured!");} else {printf ("Network ordered integer addr:% # lx\ n", conv_addr);} system ("pause"); return 0;}
Operational consequences:
Network ordered integer addr: 0x4030201
Error occured!
As can be seen from the running consequences, inet_addr () can not only convert IP addresses into 32-bit integers, but also detect valid IP addresses.
Note: when assigning values to sockaddr_in members, you need to explicitly convert the host byte order to the collection byte order, while the TCP protocol will actively convert it to the collection byte order when sending data through write () / send (). There is no need to misappropriate the response function.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.