Original article address:
Http://www.linuxgraphics.cn/gui/ipc_unix_socket.html,Thanks to the original author.
Introduction
The Client/Server structure in the GUI system is usually implemented based on UNIX domain socket. For example, X
In the window system, before connecting to the X11 server, X11 customers first determine the host where the X11 server is located based on the settings of environment variables such as display. If the host is the same host, the Unix domain socket is used to connect to the server.
Basic Unix domain socket Process
Shows the basic process of communication using Unix domain socket:
Socket
Socket () creates an endpoint for communication and returns a descriptor.
Bind
BIND () gives the socket sockfd the local address my_addr. It is
Normally necessary to assign a local address using bind () before
A sock_stream socket may receive connections.
Listen
To accept connections, a socket is first created with socket (),
A willingness to accept incoming connections and a queue limit
Incoming connections are specified with listen (), and then
Connections are accepted with accept. The listen () call applies only
To sockets of Type sock_stream or sock_seqpacket.
Accept
The accept () system call is used with connection-based socket
Types (sock_stream, sock_seqpacket). It extracts the first
Connection Request on the queue of pending connections, creates
New connected socket, and returns a new file descriptor referring to that socket.
Connect
The CONNECT () system call connects the socket referred to by
File descriptor sockfd to the address specified by serv_addr.
Read and Write
After establishing a connection between two processes that communicate with each other, the data can be read and written through the Read and Write Functions.
Between read and write processes, the operating system kernel provides a data buffer.
When a function writes data, the data is written to the data buffer. When the READ function is called to read data
Obtain data. When the buffer zone is empty, the READ function will wait until there is data in the buffer zone. When the buffer is full, the write function waits until the buffer has free space.
Use with select
UNIX domain socket programming is often used with select. The select function is used to listen to sockets. When there are connection requests or existing connections have data to be read and written
Use the accept function to accept connection requests and establish connections. Call read/write to complete data read/write.
You can use fdsets and its interfaces to monitor multiple file descriptors.
Returns the number of file descriptors in ready state. The fd_isset interface is used to determine the number of file descriptors.
Whether the Delimiter is ready.
Sample Code
- Socket.tar.gz
This code implements the number of mutual reads and writes by two processes using Unix domain socket
Data.
Other reference urls:
IPC: Sockets
Http://www.cs.cf.ac.uk/Dave/C/node28.html
Example Using Unix domain stream sockets
Http://docs.hp.com/en/B2355-90136/ch06s07.html
Important references:
Advanced Programming in UNIX environment chapter 17th Richard Steven s