Socket distinction communications domain AF_INET and AF_UNIX

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

1. AF_INET domain socket communication process

AF_INET area communication process

A typical communication process TCP / IP four-layer model.

 

Sender, recipient dependent on IP: Port to identify upcoming local socket bound to the corresponding IP port, send data, specify the partner's IP port, through Internet, you can find the ultimate recipient in accordance with the IP port; receiving data when, may be obtained from the packet sender to IP port.

The sender through the system call send () to send the raw data to the operating system kernel buffer. From top to bottom through the kernel buffer, encoding the link layer of the TCP layer, an IP layer, respectively corresponding to the header information is added, via a network card to the packet transmission network. Routed through the network card to the recipient. NIC via system interrupt data packet to the operating system notifies the receiving side, and then decodes the coded sender in the opposite direction, i.e., sequentially passes through the link layer, IP layer, TCP layer was removed header, checksum, etc., will eventually raw data reported to the recipient process.

2. AF_UNIX domain socket communication process 

The IPC typical local, like pipes, dependent on the path name identifies the sender and receiver. I.e., when data is transmitted, the intended recipient of the binding path name, the operating system can be found based on the path name corresponding to the receiving side directly, and the raw data is directly copied to the kernel buffer in the receiver, and the receiver processes reported to be deal with. The same receiver may be acquired from the received packet to the path name of the sender, and send data by using the path name.

 AF_UNIX area communication process

3. The same point

Operating system provides an interface socket (), bind (), connect (), accept (), send (), recv (), and select them for event detection multiplexed (), poll (), the epoll () are identical. Data transmission and reception process, the underlying upper application imperceptible differences. 

4. Different points

Establishing a socket address field is transmitted, and bind () of the address structure is slightly different:

  socket () respectively passing different domains AF_INET and AF_UNIX

  bind () the address of the structure were sockaddr_in (developing IP port) and sockaddr_un (specify the path name)

2 AF_INET subject to encoding and decoding of a plurality of protocol layers, the system consumes cpu, and need to go through the data transfer card, network card by bandwidth limitations. AF_UNIX data arrives after the kernel buffer, specified by the kernel to find the path name corresponding to the recipient socket kernel buffer, copy the past data directly, without passing through the codec protocol layers, the system saves cpu, and without a card, the card is therefore not bandwidth limitations.

Transmission rate is much greater than 3 AF_UNIX AF_INET

3 AF_INET cross-process communication can be used not only as the machine, the same can be used for communication between different machines, which is interconnected to the network to transfer data between different machines for us. AF_UNIX can only be used for communication between the process inside the machine. 

5. usage scenarios

AF_UNIX because of its low consumption of the cpu system, the card is not limited bandwidth and high transmission rate, the machine is preferred AF_UNIX communication domain. Needless to say, AF_INET for communication between the cross machine.

Guess you like

Origin www.cnblogs.com/heluan/p/10937098.html