socketpair函数概要例如以下:
#include <sys/types.h>
#include <sys/socket.h>
int socketpair(int domain, int type, int protocol, int sv[2]);
sys/types.h文件须要用来定义一些C宏常量。sys/socket.h文件必须包括进来定义socketpair函数原型。
socketpair函数须要四个參数。他们是:
套接口的域
套接口类型
使用的协议
指向存储文件描写叙述符的指针

类型參数声明了我们希望创建哪种类型的套接口。socketpair函数的选择例如以下:
SOCK_STREAM
SOCK_DGRAM
对于socketpair函数,protocol參数必须提供为0。
參数sv[2]是接收代表两个套接口的整数数组。每个文件描写叙述符代表一个套接口,而且与还有一个并没有差别。
假设函数成功,将会返回0值。否则将会返回-1表明创建失败,而且errno来表明特定的错误号。

关于流程。socketpair()函数创建出两个进程,fork()之后这两个进程都会运行主程序中的代码,这个一定要注意!尤其是bind的时候,假设bind两次的话,那就会出错了。通常会在子进程里调用一个带死循环的函数,这样就好了。(这个情况的样例会在综合运用中解说)

一下给出个简单的样例。

// 建立socket对
       #include <sys/types.h>
       #include <sys/socket.h>
    
       #include <stdlib.h>
       #include <stdio.h>
    
       int main ()
       {
         int fd[2];
   
        int r = socketpair( AF_UNIX, SOCK_STREAM, 0, fd );
        if ( r < 0 ) {
          perror( "socketpair()" );
          exit( 1 );
        }
   
        if ( fork() ) {
          /* Parent process: echo client */
          int val = 0;
          close( fd[1] );
          while ( 1 ) {
            sleep( 1 );
            ++val;
            printf( "Sending data: %d/n", val );
            write( fd[0], &val, sizeof(val) );
            read( fd[0], &val, sizeof(val) );
            printf( "Data received: %d/n", val );
          }
        }
        else {
          /* Child process: echo server */
          int val;
          close( fd[0] );
          while ( 1 ) {
            read( fd[1], &val, sizeof(val) );
            ++val;
            write( fd[1], &val, sizeof(val) );
          }
        }
      }

在给出一个用sendmsg来传递数据的样例

/*****************************************
 *
 * Listing 1.2
 *
 * Example performing I/O on s socket pair:
 *
 * ******************************************/
#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;
}

socketpair的使用的更多相关文章

  1. socketpair理解

    转载:http://liulixiaoyao.blog.51cto.com/1361095/533469/ 今天跟人谈到socketpair的问题,晚上回来写了个程序验证下自己的猜测! 先说说我的理解 ...

  2. [转] IPC之管道、FIFO、socketpair

    管道和FIFO作为最初的UNIX IPC形式,现在已用得较少.SocketPair可作为全双工版本的管道,较为常用,这里简单做个笔记 管道 * 只用于有亲缘关系的进程间通信 * 单向,即半双工 (双向 ...

  3. socketpair创建双向通信的管道(全双工通信)

    Linux下socketpair介绍: socketpair创建了一对无名的套接字描述符(只能在AF_UNIX域中使用),描述符存储于一个二元数组,例如sv[2] .这对套接字可以进行双工通信,每一个 ...

  4. UNIX网络编程——UNIX域套接字编程和socketpair 函数

    一.UNIX Domain Socket IPC socket API原本是为网络通讯设计的,但后来在socket的框架上发展出一种IPC机制,就是UNIX Domain Socket.虽然网络soc ...

  5. c/c++ linux 进程间通信系列3,使用socketpair,pipe

    linux 进程间通信系列3,使用socketpair,pipe 1,使用socketpair,实现进程间通信,是双向的. 2,使用pipe,实现进程间通信 使用pipe关键点:fd[0]只能用于接收 ...

  6. 输入系统:进程间双向通信(socketpair+binder)

    一.双向通信(socketpair) socketpair()函数用于创建一对无名的.相互连接的套接子,如果函数成功,则返回0,创建好的套接字分别是sv[0]和sv[1]:否则返回-1,错误码保存于e ...

  7. socketpair初识

    #include <stdio.h>  #include <string.h>  #include <unistd.h>  #include <sys/typ ...

  8. Libevent学习之SocketPair实现

    Libevent设计的精化之一在于把Timer事件.Signal事件和IO事件统一集成在一个Reactor中,以统一的方式去处理这三种不同的事件,更确切的说是把Timer事件和Signal事件融合到了 ...

  9. UNIX域套接字编程和socketpair 函数

    一.UNIX Domain Socket IPC socket API原本是为网络通讯设计的,但后来在socket的框架上发展出一种IPC机制,就是UNIX Domain Socket.虽然网络soc ...

随机推荐

  1. ABAP - 日期格式转换 &amp; ABAP经常使用日期处理函数

    ABAP - 日期格式转换 如今提供下面一些日期格式转换的函数: Below are several FMs which can be used to convert date format. 1. ...

  2. markdown 书写代码

    近期基于github + hexo 搭建了自己的博客.開始用markdown写博客,推荐 mac 平台用 mou 这个软件或者 vim. 介绍下markdown语法插入代码的规则: 有一种方法是全部代 ...

  3. 文件系统 busybox and initramfs

    1.busybox制作根文件系统 http://wenku.baidu.com/link?url=h2m_xrj6OsLiHVVhMY2e0C7WKikw_H3dZY_b4mUiW1E7AEf_q34 ...

  4. oracle安装、配置、卸载、错误解决

    oracle安装卸载的帖子很多,这里整理出一份,都只是给出一个链接,忘了时可以自己看看.哈哈,其实我也觉得已经不会忘了,被这个鸡毛问题困了两天,修改控制文件.环境变量.注册表什么的都不能解决问题,最后 ...

  5. Android ---------- 清单文件中Activity常规设置

    <activity android:name="xxxxx" android:alwaysRetainTaskState="true" android:c ...

  6. jdk内存

  7. OpenCV——Haar-like特征

    Haar-like特征--即Haar特征,是计算机视觉领域一种常用的特征描述算子.它最早用于人脸描述. 目前常用的Haar-like特征可以分为以下几类:线性特征.边缘特征.点特征(中心特征).对角线 ...

  8. Django学习(五) 定义视图以及页面模板

    请求解析一般都是通过请求的request获取一定参数,然后根据参数做一定业务逻辑判断,这其中可能包括查询数据库,然后将需要返回的数据封装成一个HttpResponse返回. 代码如下: 这是一个简单的 ...

  9. 裸kmp算法

    Number Sequence Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and ...

  10. What is an http upgrade?

    HTTP Upgrade is used to indicate a preference or requirement to switch to a different version of HTT ...