/* Orion Lawlor's Short UNIX Examples, olawlor@acm.org 2006/11/01 (Public Domain)

Send and receive data over a UNIX socket.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/socket.h> /* for socketpair */
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h> /* for wait */


int main() {
	pid_t pid;
	int pair[2];
	if (-1==socketpair(AF_UNIX,SOCK_STREAM,0,pair)) {perror("socketpair stdout"); exit(1);}
	int A=pair[0];
	int B=pair[1];
	char buf[10];
	
	send(A,"foo",4,0);
	recv(B,buf,4,0);
	printf("Received '%s' from B after putting 'foo' into A\n",buf);
	
	if ((pid=fork())==0) {/* child process */
		send(B,"bar",4,0);
		exit(0);
	} else { /* parent process */
		recv(A,buf,4,0);
		printf("Parent: received '%s' from A after child put 'bar' into B\n",buf);	
		waitpid(pid,0,0); /* keep child from becoming a zombie */
	}
	
	return 0;
}
/*<@>
<@> ******** Program output: ********
<@> Received 'foo' from B after putting 'foo' into A
<@> Received 'foo' from B after putting 'foo' into A
<@> Parent: received 'bar' from A after child put 'bar' into B
<@> */
