#include<stdio.h>#include<unistd.h>#include<sys/socket.h>#include<string.h>#define CHAR_BUFSIZE 50
intmain(intargc,char**argv){intfd[2],len;charmessage[CHAR_BUFSIZE];if(socketpair(AF_LOCAL,SOCK_STREAM,0,fd)==-1){return1;}/* If you write to fd[0], you read from fd[1], and vice versa. *//* Print a message into one end of the socket. */snprintf(message,CHAR_BUFSIZE,"A message written to fd[0]");write(fd[0],message,strlen(message)+1);/* Print a message into the other end of the socket. */snprintf(message,CHAR_BUFSIZE,"A message written to fd[1]");write(fd[1],message,strlen(message)+1);/* Read from the first socket the data written to the second. */len=read(fd[0],message,CHAR_BUFSIZE-1);message[len]='\0';printf("Read from fd[0]: %s \n",message);/* Read from the second socket the data written to the first. */len=read(fd[1],message,CHAR_BUFSIZE-1);message[len]='\0';printf("Read from fd[1]: %s \n",message);}
Linux 抽象Socket命名空间:将sun_path字段的第一个字节指定为null字节(\0)就可创建抽象绑定,不会在文件系统上创建该名字。