本地套接字

int socketpair(int domain, int type, int protocol, int sv[2]);
socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd);

可以实现父子进程通信

#include <sys/un.h>

int main()
{
    int ret = 0;
    int fd = 0;
    char *path = "/tmp/path";

    fd = socket(PF_UNIX,SOCK_STREAM,0); 
    if(fd < 0)
    {
        perror("socket");
        return -1;
    }

    struct sockaddr_un su;
    su.sun_family = AF_UNIX;
    strcpy(su.sun_path, path);

    unlink(path); //删除文件

    ret = bind(fd ,(struct sockaddr*)&su, sizeof(su));
    if(ret < 0)
    {
        perror("bind");
        return -1;
    }

    ret = listen(fd, 10);
    if(ret < 0)
    {
        perror("listen");
        return -1;
    }

    int len = 0;
    struct sockaddr_un asu;
    int afd = accept(fd, (struct sockaddr*)&asu, &len);
    if(afd < 0)
    {
        perror("accept");
        return -1;
    }

    return 0;
}
posted @ 2016-07-14 23:11  thomas_blog  阅读(107)  评论(0编辑  收藏  举报