Advanced I/O-socketpair
Socketpair
Socketpair: compared to the previously mentioned pipeline, socketpair is a full-duplex communication mode, one end of which can be read or written. I understand it like this:
Suppose we are using socketpair locally, the client is fd [0] and the server is fd [1]. When the server writes data to the client, it writes data from the writer of fd [1] and reads the data from the reader of fd [1], and vice versa.
The following is an implementation of local interprocess communication for socketpair
# include / * See NOTES * / # include int socketpair (int domain, int type, int protocol, int sv [2])
Domian is the way to operate, so we use AF_LOCAL because we are local.
Type is the transmission mode, and we use tcp's streaming service, SOCK_STREAM.
For protocol control, we choose 0 by default.
Sv for how many file descriptors to create, note (this file descriptor is a network file descriptor, it can be said to be virtual).
1 # include 2 # include 3 # include 4 # include 5 # include 6 int main () 7 {8 int fd [2]; 9 if (socketpair (AF_LOCAL,SOCK_STREAM,0,fd) 0) 41 {42 buf [size] ='\ 0mm; 43 printf ("child say::%s\ n", buf) 44} 45 memset (buf,'\ 0buf, parent hello world); 46 strcpy (buf, "parent hello world"); 47 write (fd [0], buf,strlen (buf) + 1); 48} 49 close (fd [0]); 50 51} 52 53 return 0; 54}
Socketpair implements full-duplex communication.