Post your answer

What This Function Socketpair() Does

Posted by Randy

16 Mar, 2012

Socket Creation Using socketpair()#include &ltsys/types.h>#include &ltsys/socket.h>int socketpair(int af, int type, int protocol, int sv[2])socketpair() results in the creation of two connected sockets. sv[] is the array where the file descriptors for the sockets are returned. Each descriptor in sv[] is associated with one end of the communications link. Each descriptor can be used for both input and output. This means that full two-way communication between a parent process and one child process is possible.Normally, one descriptor is reserved for use by a parent process and the other descriptor is used by a child process. The parent process closes the descriptor used by the child process. Conversely, the child process closes the descriptor used by the parent process. fork() is still required to pass one of the sockets to a child.af represents the domain or address family to which the socket belongs. type is the type of socket to create.Domains refer to the area where the communicating processes exist. Commonly used domains include: * AF_UNIX for communication between processes on one system; * AF_INET for communication between processes on the same or different systems using the DARPA standard protocols (IP/UDP/TCP). Socket type refers to the "style" of communication. The two most commonly used values include: * SOCK_STREAM: A stream of data with no record boundaries. Delivery in a networked environment is guaranteed; if delivery is impossible, the sender receives an error indicator. * SOCK_DGRAM: A stream of records, each of a given size. Delivery in a networked environment is not guaranteed. A protocol value of 0 is very common. This permits the system to choose the first protocol which is permitted with the pair of values specified for family and type.Sample Code#define DATA1 "test string 1"#define DATA2 "test string 2"#include &ltsys/types.h>#include &ltsys/socket.h>#include &ltstdio.h>#include &lterrno.h>main(){ int sockets[2], child; char buf[1024]; /* Get the socket pair */if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) < 0) { printf("error %d on socketpairn", errno); exit(1); } /* create child process */ if ((child = fork()) == -1) { printf("fork error %dn", errno); exit(1); } if (child != 0) { /* this is the parent */ /* close child's end of socket */ close(sockets[0]); /* read message from child */ if (read(sockets[1], buf, sizeof(buf)) < 0) { printf("error %d reading socketn", errno); exit(1); } printf("-->%sn", buf); /* write message to child */ if (write(sockets[1], DATA1, sizeof(DATA1)) < 0) { printf("error %d writing socketn", errno); exit(1); } /* finished */ close(sockets[1]); } else { /* the child */ /* close parent's end of socket */ close(sockets[1]); /* send message to parent */ if (write(sockets[0], DATA2, sizeof(DATA1)) < 0) { printf("error %d writing socketn", errno); exit(1); } /* get message from parent */ if (read(sockets[0], buf, sizeof(buf)) < 0) {printf("error %d reading socketn", errno); exit(1); } printf("-->%sn", buf); /* finished */ close(sockets[0]); }}