AF_INET domain and AF_UNIX domain socket communication principle

AF_INET is used for non-native communication

          The communication process of the AF_INET domain socket is a typical communication process of the TCP/IP four-layer model, as follows:

AF_INET domain communication process

       The sender and receiver rely on IP:Port to identify, that is, bind the local socket to the corresponding IP port. When sending data, specify the IP port of the other party. After the Internet, the receiver can be finally found based on this IP port; receive data At the time, the IP port of the sender can be obtained from the data packet.

       The sender sends the original data to the operating system kernel buffer through the system call send(). The kernel buffer goes through the TCP layer, the IP layer, and the link layer sequentially from top to bottom. The corresponding header information is added respectively, and a data packet is sent to the network through the network card. Route through the network to the receiver's network card. The network card notifies the data packet to the receiver’s operating system through the system interrupt, and then decodes it in the opposite direction of the sender’s encoding, that is, through the link layer, the IP layer, and the TCP layer to remove the header, check the verification, etc., and finally The original data is reported to the receiver process.

AF_INET is used as native communication

      Let me start with the conclusion: do not use the network card, do not use the physical device, but use the virtual device, loopback device loopback.

     The message path of this machine is like this: application layer -> socket interface -> transport layer (tcp/udp message) -> network layer -> back to transport layer -> backto socket interface -.> return to the application

      At the network layer, routing is queried in the routing table. The routing table (software routing, real forwarding needs to rely on hardware routing, where routing table includes fast forwarding table and FIB table) will save the host route (host route, or loop route) during initialization. ), Query (first match the mask, then match the ip, the localhost route is at the top of the routing table, the first is the first to find). It is found that there is no need to interrupt without forwarding, no need to send to the link layer, and no need to send to the network device (network card). Like the network card sending and receiving messages, the same receiving process is followed, except that the net device is a loopback device and is finally sent back to the application. This set of procedures is of course the same as forwarding and receiving external network packets, which must be processed by the kernel protocol stack. The difference is that the local address does not need to be linked to the net device.

AF_UNIX native communication

        A typical local IPC, similar to a pipe, relies on path names to identify the sender and receiver. That is, when sending data, specify the path name bound by the receiver, the operating system can directly find the corresponding receiver based on the path name, and copy the original data directly to the receiver’s kernel buffer, and report it to the receiver process. deal with. The same receiver can obtain the sender's path name from the received data packet, and send data to it through this path name.

AF_INET and AF_UNIX communication comparison

Same point

       The interface socket(), bind(), connect(), accept(), send(), recv() provided by the operating system, and select(), poll() for multiplexing event detection, epoll() is exactly the same. In the process of sending and receiving data, the upper-layer application cannot perceive the difference of the lower-layer.

difference

1 Establish the address domain passed by the socket, and the address structure of bind() is slightly different:

        Socket() passes different domains AF_INET and AF_UNIX respectively

        The address structure of bind() is sockaddr_in (specify IP port) and sockaddr_un (specify path name)

2 AF_INET needs to go through the encoding and decoding of multiple protocol layers, consumes the system CPU, and data transmission needs to go through the network card, which is limited by the bandwidth of the network card. After the AF_UNIX data arrives in the kernel buffer, the kernel finds the kernel buffer corresponding to the receiver socket according to the specified path name, and directly copies the data without going through the protocol layer encoding and decoding, saving the system cpu, and not going through the network card, so it is not affected by the network card. Bandwidth limitation.

3 The transfer rate of AF_UNIX is much greater than AF_INET

4 AF_INET can not only be used for cross-process communication of this machine, but also can be used for communication between different machines. It is born for the purpose of network interconnection and data transfer between different machines. And AF_UNIX can only be used for communication between processes within the machine.

scenes to be used

        Since AF_UNIX consumes less system CPU, it is not limited by the network card bandwidth, and the efficient transfer rate, the AF_UNIX domain is preferred for local communication.

        Needless to say, AF_INET is used for cross-machine communication.

Guess you like

Origin blog.csdn.net/sy_123a/article/details/108379232