Comparison of socket communication principle between af_inet domain and Af_unix domain

Source: Internet
Author: User

Turn from

http://blog.csdn.net/sandware/article/details/40923491

1. af_inet Domain Socket Communication process

Typical TCP/IP four-tier model communication process.

Sender, receiver relies on ip:port to identify, the local socket bound to the corresponding IP port, when sending data, specify the other IP port, through the Internet, you can eventually find the receiver based on this IP port; When the data is received, The IP port of the sender can be obtained from the packet. The sender sends the raw data to the operating system kernel buffer through the system call to send (). The kernel buffers are encoded by TCP layer, IP layer and link layer from top to bottom, and the corresponding header information is added respectively, and a packet is sent to the network by the NIC. The network card that is routed to the receiving party. The network card notifies the receiver's operating system by the system interrupt, then decodes it in the opposite direction of the sender code, i.e., through the link layer, the IP layer, the TCP layer to remove the head, check the checksum, and finally escalate the original data to the receiver process.

2. Af_unix Domain Socket Communication process

A typical local IPC, similar to a pipeline, relies on the path name to identify the sender and receiver. That is, when sending data, specify the path name of the receiver binding, the operating system can directly locate the corresponding receiver according to the pathname, and copy the original data directly to the receiver's kernel buffer, and escalate to the receiver process for processing. The same receiver can obtain the path name of the sender from the received packet and send the data to it through this pathname.

3. The same point

The operating system provides the interface socket (), bind (), connect (), accept (), send (), recv (), and the Select (), poll (), Epoll (), which is used for multiplexing event detection, is identical. In the process of sending and receiving data, the upper application perceives no underlying differences.

4. Different points

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

Sockets () pass different domains af_inet and Af_unix, respectively

The address structure of BIND () is sockaddr_in (IP port) and Sockaddr_un (specified path name), respectively

2 af_inet needs to pass through several protocol layer codec, consumes the system CPU, and the data transmission needs to pass through the network card, by the network card bandwidth limit. After the Af_unix data reaches the kernel buffer, the kernel buffer corresponding to the receiver socket is found according to the specified path name, and the data is copied in the past without the protocol layer codec, which saves the system CPU and does not pass through the network card, so it is not limited by the network card bandwidth.

3 Af_unix transfer rate is much larger than af_inet

3 Af_inet can not only be used as the cross-process communication of the machine, but also can be used for communication between different machines in order to transmit data between different machines for network interconnection. The Af_unix can only be used for communication between processes within the machine.

5. Usage Scenarios

Af_unix because of its low CPU consumption of the system, not limited by network card bandwidth, and efficient transfer rate, native communication preferred Af_unix domain.

Needless to say, af_inet is used to communicate across machines.

Comparison of socket communication principle between af_inet domain and Af_unix domain

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.