Create a pair of UNIX domain sockets by using the socketpair API

You can use the socketpair function to create a pair of connected UNIX domain sockets. The pair of sockets is unnamed; that is, they are not bound to a file path.

When you issue the socketpair function, the z/TPF system creates a pair of UNIX domain sockets and establishes a connection between the sockets. You do not need to issue any other socket application programming interface (API) such as the socket, bind, listen, accept, or connect function for the sockets before you can send or receive data.

Restriction: On the z/TPF system, sockets that are created by using the socketpair function support only the stream-oriented communication.

Example: socketpair communication

The following example shows the communication between a pair of sockets that are created by using the socketpair function:
	#include <sys/socket.h>
	#define CHILDDATA “Hello parent, from child\n”

	int sockets[2], rc; 	/* integer array for sockets to be returned in */
	pid_t pid;
	char buf[256];
	
	rc = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
	if (rc == -1) {
		printf(“ERROR OPENING SOCKET PAIR = %d\n”, sock_errno());
		exit(0);
	}

	pid = fork();
	if (pid == -1){
		printf(“FORK ERROR\n”);
	}
	else if (pid == 0){		/* this is the child */
		close(sockets[0]);      /* close the parent’s socket */
		rc = write(sockets[1], CHILDDATA, sizeof(CHILDDATA)); 
                                    /* write to parent */
		if (rc == -1){
			printf(“ERROR ON WRITE = %d\n”, sock_errno());
		} 
		do_child_process_work(sockets[1]);	 
                                    /* do work on socket in child process */
	else {				/* this is the parent */
	      close(sockets[1]);      /* close the child’s socket*/
		rc = read(sockets[0], buf, sizeof(buf));
		if (rc == -1){
			printf(“ERROR ON READ = %d\n”, sock_errno());
		}
		printf(“%s\n”, buf);	/* print what was read */
		do_parent_process_work(sockets[0]);
                                    /* do work on socket in parent process */
	}