LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 449|回复: 0

ibm的一个sendmsg的例子,编译遇到一些问题

[复制链接]
发表于 2012-3-12 16:13:17 | 显示全部楼层 |阅读模式
原地址
http://publib.boulder.ibm.com/in ... 2Fx1descriptors.htm

我在编译的时候有几个位置都报错,我想应该是结构体的头文件没加
但是不知道缺少哪几个?

代码如下:

  1. /**************************************************************************/
  2. /* Server example that uses sendmsg() to create worker jobs               */
  3. /**************************************************************************/
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <spawn.h>

  9. #define SERVER_PORT  12345

  10. main (int argc, char *argv[])
  11. {
  12.    int    i, num, pid, rc, on = 1;
  13.    int    listen_sd, accept_sd;
  14.    int    server_sd, worker_sd, pair_sd[2];
  15.    int    spawn_fdmap[1];
  16.    char  *spawn_argv[1];
  17.    char  *spawn_envp[1];
  18.    struct inheritance   inherit;
  19.    struct msghdr        msg;
  20.    struct sockaddr_in6   addr;

  21.    /*************************************************/
  22.    /* If an argument was specified, use it to       */
  23.    /* control the number of incoming connections    */
  24.    /*************************************************/
  25.    if (argc >= 2)
  26.       num = atoi(argv[1]);
  27.    else
  28.       num = 1;

  29.    /*************************************************/
  30.    /* Create an AF_INET6 stream socket to receive   */
  31.    /* incoming connections on                       */
  32.    /*************************************************/
  33.    listen_sd = socket(AF_INET6, SOCK_STREAM, 0);
  34.    if (listen_sd < 0)
  35.    {
  36.       perror("socket() failed");
  37.       exit(-1);
  38.    }

  39.    /*************************************************/
  40.    /* Allow socket descriptor to be reuseable       */
  41.    /*************************************************/
  42.    rc = setsockopt(listen_sd,
  43.                    SOL_SOCKET,  SO_REUSEADDR,
  44.                    (char *)&on, sizeof(on));
  45.    if (rc < 0)
  46.    {
  47.       perror("setsockopt() failed");
  48.       close(listen_sd);
  49.       exit(-1);
  50.    }

  51.    /*************************************************/
  52.    /* Bind the socket                               */
  53.    /*************************************************/
  54.    memset(&addr, 0, sizeof(addr));
  55.    addr.sin6_family      = AF_INET6;
  56.    memcpy(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any));
  57.    addr.sin6_port        = htons(SERVER_PORT);
  58.    rc = bind(listen_sd,
  59.              (struct sockaddr *)&addr, sizeof(addr));
  60.    if (rc < 0)
  61.    {
  62.       perror("bind() failed");
  63.       close(listen_sd);
  64.       exit(-1);
  65.    }

  66.    /*************************************************/
  67.    /* Set the listen back log                       */
  68.    /*************************************************/
  69.    rc = listen(listen_sd, 5);
  70.    if (rc < 0)
  71.    {
  72.       perror("listen() failed");
  73.       close(listen_sd);
  74.       exit(-1);
  75.    }

  76.    /*************************************************/
  77.    /* Create a pair of UNIX datagram sockets        */
  78.    /*************************************************/
  79.    rc = socketpair(AF_UNIX, SOCK_DGRAM, 0, pair_sd);
  80.    if (rc != 0)
  81.    {
  82.       perror("socketpair() failed");
  83.       close(listen_sd);
  84.       exit(-1);
  85.    }
  86.    server_sd = pair_sd[0];
  87.    worker_sd = pair_sd[1];

  88.    /*************************************************/
  89.    /* Initialize parms before entering for loop   */
  90.    /*                                               */
  91.    /* The worker socket descriptor is mapped to     */
  92.    /* descriptor 0 in the child program.            */
  93.    /*************************************************/
  94.    memset(&inherit, 0, sizeof(inherit));
  95.    spawn_argv[0]  = NULL;
  96.    spawn_envp[0]  = NULL;
  97.    spawn_fdmap[0] = worker_sd;

  98.    /*************************************************/
  99.    /* Create each of the worker jobs                */
  100.    /*************************************************/
  101.    printf("Creating worker jobs...\n");
  102.    for (i=0; i < num; i++)
  103.    {
  104.       pid = spawn("/QSYS.LIB/QGPL.LIB/WRKR2.PGM",
  105.                   1, spawn_fdmap, &inherit,
  106.                   spawn_argv, spawn_envp);
  107.       if (pid < 0)
  108.       {
  109.          perror("spawn() failed");
  110.          close(listen_sd);
  111.          close(server_sd);
  112.          close(worker_sd);
  113.          exit(-1);
  114.       }
  115.       printf("  Worker = %d\n", pid);
  116.    }

  117.    /*************************************************/
  118.    /* Close down the worker side of the socketpair  */
  119.    /*************************************************/
  120.    close(worker_sd);

  121.    /*************************************************/
  122.    /* Inform the user that the server is ready      */
  123.    /*************************************************/
  124.    printf("The server is ready\n");

  125.    /*************************************************/
  126.    /* Go through the loop once for each connection  */
  127.    /*************************************************/
  128.    for (i=0; i < num; i++)
  129.    {
  130.       /**********************************************/
  131.       /* Wait for an incoming connection            */
  132.       /**********************************************/
  133.       printf("Interation: %d\n", i+1);
  134.       printf("  waiting on accept()\n");
  135.       accept_sd = accept(listen_sd, NULL, NULL);
  136.       if (accept_sd < 0)
  137.       {
  138.          perror("accept() failed");
  139.          close(listen_sd);
  140.          close(server_sd);
  141.          exit(-1);
  142.       }
  143.       printf("  accept completed successfully\n");

  144.       /**********************************************/
  145.       /* Initialize message header structure        */
  146.       /**********************************************/
  147.       memset(&msg, 0, sizeof(msg));

  148.       /**********************************************/
  149.       /* We are not sending any data so we do not   */
  150.       /* need to set either of the msg_iov fields.  */
  151.       /* The memset of the message header structure */
  152.       /* will set the msg_iov pointer to NULL and   */
  153.       /* it will set the msg_iovcnt field to 0.     */
  154.       /**********************************************/

  155.       /**********************************************/
  156.       /* The only fields in the message header      */
  157.       /* structure that need to be filled in are    */
  158.       /* the msg_accrights fields.                  */
  159.       /**********************************************/
  160.       msg.msg_accrights  = (char *)&accept_sd;
  161.       msg.msg_accrightslen = sizeof(accept_sd);

  162.       /**********************************************/
  163.       /* Give the incoming connection to one of the */
  164.       /* worker jobs.                               */
  165.       /*                                            */
  166.       /* NOTE: We do not know which worker job will */
  167.       /*       get this inbound connection.         */
  168.       /**********************************************/
  169.       rc = sendmsg(server_sd, &msg, 0);
  170.       if (rc < 0)
  171.       {
  172.          perror("sendmsg() failed");
  173.          close(listen_sd);
  174.          close(accept_sd);
  175.          close(server_sd);
  176.          exit(-1);
  177.       }
  178.       printf("  sendmsg completed successfully\n");

  179.       /**********************************************/
  180.       /* Close down the incoming connection since   */
  181.       /* it has been given to a worker to handle    */
  182.       /**********************************************/
  183.       close(accept_sd);
  184.    }

  185.    /*************************************************/
  186.    /* Close down the server and listen sockets      */
  187.    /*************************************************/
  188.    close(server_sd);
  189.    close(listen_sd);
  190. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表