Using socketpair () full duplex communication between processes

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
/*************************************************
*  description: use socketpair() To implete IPC (full duplex IPC)
*  author: chengshuguang
**************************************************/
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>

#define child "wo shi child fa lai de"
#define parent "wo shi parent fa lai de"

int main()
{
	int fd[2];
	int ret;
	ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fd);

	pid_t pid;
	pid = fork();
	printf("here\n");
	if(pid == 0)
	{
		char buf[20];
		close(fd[0]);
		read(fd[1],buf,20);
		printf("child: %s\n",buf);

		write(fd[1],child,sizeof(child));
		close(fd[1]);

	}
	else
	{
		char buf[20];
		close(fd[1]);
		write(fd[0],parent,sizeof(child));
		read(fd[0],buf,20);
		printf("parent: %s\n",buf);
		close(fd[0]);
	}
		
	sleep(10);


	return 0;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Angelina at December 02, 2013 - 2:17 PM