Socketpair function Explanation

Source: Internet
Author: User

Let's take a look at the prototype of the Socketpair function as follows:

int socketpair (int domain,int type,int protocol,int sv[])

The first parameter represents the protocol family and must be af_local;

The second parameter represents the type, either Sock_stream, or SOCK_DGRAM, when the parameter is specified as Sock_stream, the resulting result is called the Flow pipeline, which differs from the general pipeline in that the left pipeline is full duplex, that is, two descriptors can be read and writable;

The third parameter can only be 0;

The fourth parameter is used to save the created socket pair;

The Socketpair function establishes a pair of anonymous, connected sockets, with two socket descriptors placed in sv[0] and sv[1]. Can be read from the sv[0] write sv[1], and can be read from the sv[1] sv[0] written, if not written in the school will be blocked. Purpose: Used to create full-duplex channels, but only between parent-child processes.

Let's look at the use of this function using a piece of code:

   #include  <stdio.h>    #include  <errno.h>   # include <string.h>    #include  <unistd.h>    #include   <sys/types.h>    #include  <sys/socket.h>                                                                                                                                                                                   int main ()   {      int sv[2]={0,0};      int  sock=socketpair (AF_LOCAL,SOCK_STREAM,0,SV);         if (sock<0)       {          perror (" Socketpair ");           exit (0);       }        pid_t id=fork ();       char buf[1024];      if (id<0)        {    &nbSp;     perror ("fork");           Exit (0);       }      else if (id==0)        {          close (sv[0]);   //Sub-process off read-          while (1)            {               memset (buf, ' + ', sizeof (BUF));               strcpy (buf, "I am your child");               write (Sv[1],buf,strlen (BUF));//Sub-process write                memset (buf, ' + ', sizeof (BUF));          &nbSp;    ssize_t _s=read (sv[1],buf,sizeof (BUF)-1);//child process reads the contents of the parent process                buf[_s]= ';               printf ("father-->child:%s\n", buf);               sleep (1);           }          close (sv[1]);//Sub-process close read       }      else       {//Parent Process           close (sv[1]);           while (1)           {               memset (buf, ' I ', sizeof (BUF )); &NBSP;&NBSP;&NBSP;&NBSP;&NBsp;         ssize_t _s=read (sv[0],buf,sizeof (BUF)-1);                                                                                                                                    buf[_s]= ' + ';               printf ("Child-->father: %s\n ", buf);              memset (buf, ' + ', sizeof (BUF));               strcpy (buf, "I am your  father ");               write (Sv[0] , Buf,strlen (BUF));               sleep (1) ;          }           close (sv[0]);      }      return  0;  }

Let's look at the results of the operation:

650) this.width=650; "title=" V71b3dpxxu4en_12}cd~nyi.png "src=" Wkiol1dbmiwwl6yvaaa41_vtywq542.png "alt=" Wkiol1dbmiwwl6yvaaa41_vtywq542.png "/>

We can see that the parent-child process through the Socketpair function created by the full-duplex pipeline to achieve inter-process communication, each other can send and receive information, but note: The parent-child process in communication, must close a description symbol, because one at the time of writing the other can only read.

Socketpair function Explanation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.