Introduction to Linux Basics-Interprocess Communication-Socket

1. Local inter-process communication

Socketpair can realize the mechanism of native inter-process communication, similar to an unnamed pipe, but it can realize two-way communication, and the communication method belongs to the socket network communication category. Its function is declared as follows:
extern int socketpair(int domain,int type, int protocol,int fds[2]) The
first parameter domain is used to specify the address cluster or protocol cluster used by the socket object, AF_UNIX implements native data stream transmission, and
the second parameter type is used to specify the data stream method TCP/UDP
The third parameter protocol is used to specify the protocol IPv4 or IPv6
. The fourth parameter specifies a local array member (similar to the parameter of the pipe function that creates a pipe)

2. Realize that both ends send/receive data to each other

Server side:
(1) Call the socket function, create a socket object, specify the communication protocol as AF_UNIX
(2) Call the bind function, bind the created socket object with the file of the socket type (including the server address)
(3) Call the listen function, The socket object is in the listening state, and the listening queue size is set
(4) After waiting for the client to send a request, the server listens to the request, the accept function accepts the request and returns a new file descriptor, thereby establishing a connection
(5) The server calls the read function Receive data (in a blocking state, waiting for the client to send data), after receiving the data, output the received data
(6) call the write function to send data to the client
(7) after the communication is completed, call the close function to close the socket object
Server code

Client:
(1) Call the socket function, create a socket object, specify the same communication protocol
(2) Call the connect function, initiate a connection request to the server
(3) After getting the server's permission, first call the write function to send a message to the server
( 4) Call the read function to accept the data
(5) After the communication is complete, call the close function to close the socket object
Client code

3. Communication flow chart

Communication flow chart
TCP three-way handshake
Reminder:
If there is any unclear description in the above article, please comment in the comment area. If you have time, we will reply as soon as possible, thank you!

Guess you like

Origin blog.csdn.net/qq_20677327/article/details/104328994