UNIX domain socket IPC-blueliuyun column-blog channel-csdn. net
4. UNIX domain socket IPC
The socket API was originally designed for network communication, but later an IPC Mechanism was developed on the socket framework, namely the Unix domain socket. Although the network socket can also be used for inter-process communication between the same host (through the loopback address 127.0.0.1), the Unix domain socket is more efficient for IPC:It does not need to pass through the network protocol stack, and does not need to package or unpack, calculate the checksum, maintain the serial number and response,Only copy application layer data from one process to another. This is because,The IPC Mechanism is essentially reliable communication, and the network protocol is designed for unreliable communication.. UNIX domain socket also provides two API interfaces, namely stream-oriented and data packet-oriented, similar to TCP and UDP. However, message-oriented Unix domain socket is reliable, and messages are neither lost nor disordered.
The Unix domain socket is full-duplex and has rich API interface semantics. Compared with other IPC Mechanisms, it has become the most widely used IPC mechanism, such as the X Window server and GUI.ProgramCommunication is through Unix domain socket.
The process of using Unix domain socket is very similar to that of network socket. You must first call socket () to create a socket file descriptor,Address Family is specified as af_unix, Type can be sock_dgram or sock_stream,The Protocol parameter is still set to 0.You can.
Programming of Unix domain socket and network socketThe most obvious difference is that the address format is different, represented by the addr_un struct.The Network Programming socket address is the IP address and port number, while the Unix domain socket address is the path of a socket-type file in the file system. This socket file is created by the BIND () call, if the file already exists when BIND () is called, The BIND () error is returned.
The following program binds a Unix domain socket to an address.