0.
A project was created some time ago, and the corresponding IPaddress needs to be parsed Based on the hostname. This function is called gethostbyname.
Find the corresponding function.
1.
The general process is as follows:
Application header file:
# Include <netdb. h>
Function and data structure:
Struct hostent * gethostbyname (const char * hostname );
The non-null pointer returned by this function points to the following hostent structure:
Struct
Hostent{
Char
* H_name;/* Official name
Host */
Char
** H_aliases;/* pointer to array
Pointers to alias names */
Int
H_addrtype;/* Host address
Type: af_inet or af_inet6 */
Int
H_length;/* length of Address: 4
Or 16 */
Char
** H_addr_list;/* PTR to array of ptrs
With IPv4 or IPv6 addrs */
};
# Define h_addr
H_addr_list [0]/* First address
In list */
In particular, the last define definition must be noted that I did not find it in the struct for a long time. It turned out to be a macro definition.
3. Other functions to be used:
INet_aton, inet_addr and inet_ntoaConvert the IPv4 address before the binary value of the 32-bit network byte string (for example, "192.168.1.10") and its 32-bit network byte,
There are two newer functionsInet_ton and inet_ntopBoth IPv4 and IPv6 addresses can be processed.
4.Network byte order and host byte order)
(1)
During network transmission, when the TCP/IP protocol saves the 32-bit binary number of the IP address, the Protocol specifies the storage order of the High-byte data contained in the low-level storage address, this sequence format is called the network byte sequence format. During actual network transmission, data is transmitted in a set of 32-bit binary numbers. Due to the storage order, the actual byte transmission order is from the high byte to the low byte transmission order.
To enable both parties to understand the binary information such as the source address, destination address, and group length carried by the Data Group, whether it is a host or a router, before each group is sent, the binary information must be converted to the standard network byte sequence format of TCP/IP. The network address in the byte sequence format is not affected by the host or router type, and its representation is unique.
In socket programming and development, the functions inet_addr and inet_ntoa can be used to convert the dot string to the IP address in the network byte sequence format.
(2)
The format of the host's byte sequence is as the name suggests. The IP address format is related to a specific host or router. For different hosts, there are different formats for storing IP addresses. For example, for Motorola 68k series hosts, HBO and nbo are the same. For intel X86 series, HBO and nbo are the opposite.
In socket programming, there are four functions to convert the host byte sequence format and network byte sequence format: htonl, htons, ntohl, and ntohs. Htons and ntohs convert each other to a 16-bit unsigned number. htonl and ntohl convert each other to a 32-bit unsigned number.
In practical applications, we often use the example of converting the port number (for example, above ). This is because if you enter a number and use this number as the port number, the application must use it before creating an address, it is converted from the host byte sequence to the network byte sequence (using the htons () function) to comply with the storage standards stipulated by the TCP/IP protocol. Correspondingly, if the application wants to display the port numbers contained in an address (for example, returned from the getpeername () function ), this port number must be converted from the network sequence to the host sequence before being displayed (using the ntohs () function ).