商务合作加Q:411239339

socketpair函数的作用

浏览:369次阅读
一条评论

共计 2557 个字符,预计需要花费 7 分钟才能阅读完成。

        今天在阅读 nginx 源码时发现 socketpair 这个函数,开始还以为是 nginx 中自定义的函数,man 一下后才发现,还有父子进程间通讯的作用,以下是该函数的说明:
SOCKETPAIR(2)              Linux Programmer’s Manual             SOCKETPAIR(2)

NAME
       socketpair - create a pair of connected sockets

SYNOPSIS
       #include <sys/types.h>          /* See NOTES */
       #include <sys/socket.h>

       int socketpair(int domain, int type, int protocol, int sv[2]);

DESCRIPTION
       The socketpair() call creates an unnamed pair of connected sockets in the specified domain, of the specified
       type, and using the optionally specified protocol.  For further details of these arguments, see socket(2).

       The descriptors used in referencing the new sockets are returned in sv[0] and sv[1].  The  two  sockets  are
       indistinguishable.

RETURN VALUE
       On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.

以下是一个父子进程通讯的例子:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>

int main(int argc,char **argv)
{
    int z;        /* Status return code */
    int s[2];    /* Pair of sockets */
 struct msghdr msg;
    struct iovec iov[1];
 char send_buf[100] = "TEST";
 struct msghdr msgr;
    struct iovec iovr[1];
    char recv_buf[100];


    /*
     * Create a pair of local sockets:
     */
    z = socketpair(AF_LOCAL,SOCK_STREAM,0,s);

    if(z == -1)
    {
        fprintf(stderr,
                "%s:socketpair(AF_LOCAL,SOCK_STREAM,""0)/n",strerror(errno));
        return 1;    /* Failed */
    }

    /*
     * Sendmsg s[1]:
     */

         bzero(&msg, sizeof(msg));
         msg.msg_name = NULL;        /* attention this is a pointer to void* type */
         msg.msg_namelen = 0;
         iov[0].iov_base = send_buf;
         iov[0].iov_len = sizeof(send_buf);
         msg.msg_iov = iov;
         msg.msg_iovlen = 1;

    printf("sendmsg begin./n");
   z = sendmsg( s[1], &msg, 0 );
   if(z == -1 )
   {    fprintf(stderr,"Sendmsg failed.  errno : %s/n",strerror(errno));
    return -1;
   }
    printf("Sendmsg Success!/n");

    /*
     * Read from socket s[0]:
     */

         bzero(&msg, sizeof(msg));
         msgr.msg_name = NULL;        /* attention this is a pointer to void* type */
         msgr.msg_namelen = 0;
         iovr[0].iov_base = &recv_buf;
         iovr[0].iov_len = sizeof(recv_buf);
         msgr.msg_iov = iovr;
         msgr.msg_iovlen = 1;

         z = recvmsg(  s[0], &msgr, 0);
   if(z == -1 )
   {    fprintf(stderr,"Recvmsg failed.  errno : %s/n",strerror(errno));
    return -1;
   }
    printf("Recvmsg Success!/n");
 printf("recvmsg : %s/n", recv_buf);

    /*
     * Close the sockets:
     */
    close(s[0]);
    close(s[1]);

    puts("Done");
    return 0;
}

正文完
扫码赞助
post-qrcode
 0
果子
版权声明:本站原创文章,由 果子 于2014-09-25发表,共计2557字。
转载说明:除特殊说明外本站文章皆由果较瘦原创发布,转载请注明出处。
评论(一条评论)
hellow
2014-09-28 23:48:57 回复

Very good

 Windows  Chrome  中国湖南省长沙市铁通