. 4. IP address, successor part
First, we have used structsockaddr_in ina, for example, "10.12.110.57" or "2001: db8: 63b3: 1: 3490". How can we store it? We use inet_ton () to convert to the structure. ("Pton" is short for presentation to network .)
The following code is based on IPv4 and IPv6 respectively:
Struct sockaddr_insa; // IPv4
Structsockaddr_in6 sa6; // IPv6
Inet_ton (AF_INET, "192.0.2.1", & (sa. sin_addr); // IPv4
Inet_pton (AF_INET6, "2001: db8: 63b3: 1: 3490", & (sa6.sin6 _ addr); // IPv6
(Note: The old method is to use inet_addr () or inet_ton (); but they cannot work on IPv6)
However, the above Code is not robust because there is no error check. For example, if inet_ton () is returned,-1 indicates an error, or 0 indicates that the address does not exist. Therefore, check the return value.
Okay. Now we convert the IP address string to the binary format. What about the opposite? How can we convert struct in_addr to a point-based IP address? (Or convert to colon hexadecimal-Ipv6) Here, you can use the inet_ntop () function (ntop means network to presentation), as shown below:
When you call it, you can determine the address type (IPv4 or IPv6), always return a string pointer, and know the maximum length of the string. (Two macros do this: INET_ADDRSTRLEN and INET6_ADDRSTRLEN)
(The Conversion Function is inet_ntoa (). He cannot work on IPv6 either)
Finally, these functions can only work on Digital IP addresses.
3.4.1. Private Network
The internal Network working behind the fire wall needs to use NAT (Network Address Translation) relative to the external Network ).
Are you nervous? "What's weird ?"
All right, we can ignore these issues. They are handled by NAT.
In fact, there is a firewall at home. There are two allocated static IPv4 addresses and some computers on the network. How is this possible? The two computers cannot share these IP addresses, and how can I know where the data is from!
They cannot share these IP addresses. They are allocated private network addresses.
Some content is omitted...
For details about Private Network Address Allocation, refer to RFC1918.
Note that 192. 168. *. * is a private address in Class C address.
From the column xiaobin_HLJ80