Advanced I/O-----socketpair

Source: Internet
Author: User

Socketpair

Socketpair: Compared to the previously mentioned pipeline, Socketpair is a full-duplex mode of communication, one end of which can be read or written, for which I understand:

650) this.width=650; "Src=" http://img.blog.csdn.net/20160529004341214?watermark/2/text/ Ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center "Width=" "Height="/>

Suppose now we are using Socketpair locally, the client is fd[0], the server side is fd[1], when the server writes data to the client, writes data from the write side of the fd[1], and reads the data from the read side of the fd[1], and vice versa from the client.


The following is an implementation of a Socketpair version of interprocess communication

#include <sys/types.h>/* See NOTES */#include <sys/socket.h> int socketpair (int domain, int type, int protocol, int sv[2]);


Domian is the way to operate, because we are local so with af_local.

Type is the mode of transmission, we use the TCP streaming service, Sock_stream.

Protocol for control, we choose to fill the default 0,

SV is the number of file descriptors to create, note (this file descriptor is a network file descriptor, which can be said to be virtual).


  1  #include <stdio.h>  2  #include <sys/types.h>  3 # include<sys/socket.h>  4  #include <unistd.h>  5  #include <string.h >  6 int main ()   7 {  8     int  Fd[2];  9     if (Socketpair (AF_LOCAL,SOCK_STREAM,0,FD) <0)  10      { 11         perror ("Socketpair");  12     } 13     char buf[1024]; 14      pid_t id=fork ();  15     if (id<0) { 16          perror ("fork");  17     } Else if (id==0) { 18         close (fd[0]); 19      &Nbsp;   while (1)  20         { 21              sleep (1); 22              memset (buf, ' + ', sizeof (BUF)-1); 23              strcpy (buf, "child hello  World "),  24             write (Fd[1],buf, Strlen (BUF) +1);  25             ssize_t  size=read (fd[1],buf,sizeof (BUF)-1); 26              if (size>0)  27              { 28                  buf[size]= ' &nbs ';p;29                  printf ("parents say::%s\n", buf); 30              } 31         } 32              close (fd[1]); 33      } 34     else{ 35              close (fd[1]); 36              while (1)  37              { 38                  sleep (2); 39                  ssize_t size=read (fd[0],buf,sizeof (BUF)-1); 40                  if (size>0)  41                  { 42                      buf[size]= '  43 ';                       printf ("child say::%s\n", buf); 44                  } 45                  memset (buf, ' + ', sizeof (BUF)); 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 }


The Socketpair realizes full duplex communication mode.


This article is from the "Traces" blog, be sure to keep this source http://wpfbcr.blog.51cto.com/10696766/1784271

Advanced I/O-----socketpair

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: info-contact@alibabacloud.com 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.