Ladies and gentlemen, crossing, the last time we were talking about the size of the computer in the example, this time we say the example is the socket communication address system call . Gossip Hugh, words return to the positive. Let's talk C chestnuts together!
Crossing, we introduced the communication address of the socket in the previous Zhanghuizhong, and this time we will describe the system call that operates the socket communication address. We all know that the communication address of the socket is related to the domain of the socket, and next, we describe the communication domain of the socket separately.
Af_unix Domain Communication Address
The communication address of the Af_unix domain is essentially a directory of files, the file directory is stored on the local computer, and the communication between the two parties is on the same computer when using that domain. As you can see, both the communication address and the communication are located on the same computer, so we don't have to worry about whether the address is stored using the big-endian or the small-end method, or whether the transfer method in the communication process uses the big-endian or small-end method, because the same computer is stored in a fixed way.
Af_inet Domain Communication Address
The communication address of the af_inet domain is essentially the IP address and port number of the computer, both of which are stored on the local computer, where the two parties belong to different computers, and different computers may use different storage methods to store their IP addresses and port numbers. Whether to use the big-endian or the small-end method depends on the hardware architecture of the computer itself, but one thing is to be sure, that is, the end-of-life method is used to transmit data during communication. Therefore, when we use socket communication in this domain, we need to consider how the computer's size side storage affects communication. In fact, the system designer has already thought of this, it provides us with communication address related system calls, through these system calls can effectively solve the problem of the size side. We'll cover these system calls in more detail below.
htonl(uint32_t hostlong); htons(uint16_t hostshort); ntohl(uint32_t netlong); ntohs(uint16_t netshort);
Before we introduce these functions, let's take a few notes to make it easier for us to introduce them:
- The h in the function is the abbreviation of the host, which indicates how the data is stored in the local computer, which may be one of the big-endian or small ends.
- The N in the function is the abbreviation of the network, which means that the data is stored in the net, in fact it is the big-endian way.
- The S in the function is the abbreviation for short, which represents the short int type.
- The L in the function is an abbreviation of long, representing the long int type.
- The to in the function represents the meaning of the transformation.
Now let's take a look at the first function in these functions:
Htonl can be seen from the name, it is the conversion of H to N, more accurately, it is the use of the local computer using big-endian or small-end storage of data stored in the network used to store data in the middle-endian way. The l at the end of the function indicates that the data is of type int long. Usually we call the local computer to use the big-endian or small-end data stored in the local byte-order data, and the network in the end of the use of data stored in the network byte-order data. Therefore, the function htonl indicates that the local byte-order data is converted to network byte-order data, and the data is of type long int.
After the first function is introduced, I think everyone can infer the function of other functions:
- The function htonl () indicates that the local byte-order data is converted to network byte data, and the data is of type int long;
- The function htons () indicates that the local byte-order data is converted to network byte data, and the data is of type int short;
- The function Ntohl () indicates that the network byte-order data is converted to local byte data, and the data is of type int long;
The function Ntohs () indicates that the network byte-order data is converted to local byte data, and the data is of type int short;
These functions are located in the same header file, and you need to include the header file when you use them: #include <arpa/inet.h> . In addition, these functions are intelligent, and their intelligence is embodied in the process of data conversion: If the local byte order and network byte order are the same, then they will not be converted. It seems to be a little bit clever, haha. Let's take a look at the usage of these functions using code:
#include <stdio.h>#include <arpa/inet.h>intMain () {Long intH_data =0x12345678;Long intN_data =0;intLen =sizeof(Long int);//Get the Count of byte intI =-0;Char*p = (Char*) &h_data;printf("host data \ n");printf("[address] [value] \ n"); while(I<len) {printf("[%p] [%x] \ n", p+i,* (p+i)); i++; } N_data = Htonl (H_data);//Convert data from the host to networkp = (Char*) &n_data;printf("\ nthe network data \ n");printf("[address] [value] \ n"); I-= Len; while(I<len) {printf("[%p] [%x] \ n", p+i,* (p+i)); i++; }return 0;}
This example was modified on the basis of the previous example, except that we used the HTONL function to convert the data. The rest of the content is not described in detail, I believe that we can see clearly. In addition, we use only the HTONL function, and the usage of several other functions is exactly the same as its usage, so we will not give an example to illustrate this in one way or another.
The following is the result of the program operation, please refer to:
Host Data [Address] [value] [0xbfe19a1c] [+] [0XBFE19A1D] [+] [0XBFE19A1E] [More] [0xbfe19a1f] [] Network Data [Address] [value] [0XBFE19A20] [] [0xbfe19a21] [More] [0xbfe19a22] [+] [0xbfe19a23] [+]
As you can see from the running results above, local byte-order data and network byte-order data are stored in the opposite way, which also shows that it is necessary to use a conversion function.
You crossing, the example of socket communication address system call we're going to talk about this. I want to know what the following example, and listen to tell.
Talk C Chestnut bar (153th back: C language Instance--socket communication address system call one)