免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 8849 | 回复: 3
打印 上一主题 下一主题

tipc 示例代码 [复制链接]

论坛徽章:
84
每日论坛发贴之星
日期:2015-12-29 06:20:00每日论坛发贴之星
日期:2016-01-16 06:20:00每周论坛发贴之星
日期:2016-01-17 22:22:00程序设计版块每日发帖之星
日期:2016-01-20 06:20:00每日论坛发贴之星
日期:2016-01-20 06:20:00程序设计版块每日发帖之星
日期:2016-01-21 06:20:00每日论坛发贴之星
日期:2016-01-21 06:20:00程序设计版块每日发帖之星
日期:2016-01-23 06:20:00程序设计版块每日发帖之星
日期:2016-01-31 06:20:00数据库技术版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-14 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-04-21 17:09 |只看该作者 |倒序浏览
/* ------------------------------------------------------------------------
*
* server_tipc.c
*
* Short description: TIPC hello world demo (server side)
* ------------------------------------------------------------------------
*
* Copyright (c) 2003, Ericsson Research Canada
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of Ericsson Research Canada nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* ------------------------------------------------------------------------
*/

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

#define SERVER_TYPE  18888
#define SERVER_INST  17

int main(int argc, char *argv[], char *dummy[])
{
        struct sockaddr_tipc server_addr;
        struct sockaddr_tipc client_addr;
        socklen_t alen = sizeof(client_addr);
        int sd;
        char inbuf[40];
        char outbuf[40] = "Uh ?";

        printf("****** TIPC server hello world program started ******\n\n");

        server_addr.family = AF_TIPC;
        server_addr.addrtype = TIPC_ADDR_NAMESEQ;
        server_addr.addr.nameseq.type = SERVER_TYPE;
        server_addr.addr.nameseq.lower = SERVER_INST;
        server_addr.addr.nameseq.upper = SERVER_INST;
        server_addr.scope = TIPC_ZONE_SCOPE;

        sd = socket(AF_TIPC, SOCK_RDM, 0);

        if (0 != bind(sd, (struct sockaddr *)&server_addr, sizeof(server_addr)))
    {
                printf("Server: failed to bind port name\n");
                exit(1);
        }

        if (0 >= recvfrom(sd, inbuf, sizeof(inbuf), 0, (struct sockaddr *)&client_addr, &alen))
    {
                perror("Server: unexpected message");
        }
        printf("Server: Message received: %s !\n", inbuf);

        if (0 > sendto(sd, outbuf, strlen(outbuf)+1, 0, (struct sockaddr *)&client_addr, sizeof(client_addr)))
    {
                perror("Server: failed to send");
        }
        printf("\n****** TIPC server hello program finished ******\n");

        exit(0);
}

论坛徽章:
84
每日论坛发贴之星
日期:2015-12-29 06:20:00每日论坛发贴之星
日期:2016-01-16 06:20:00每周论坛发贴之星
日期:2016-01-17 22:22:00程序设计版块每日发帖之星
日期:2016-01-20 06:20:00每日论坛发贴之星
日期:2016-01-20 06:20:00程序设计版块每日发帖之星
日期:2016-01-21 06:20:00每日论坛发贴之星
日期:2016-01-21 06:20:00程序设计版块每日发帖之星
日期:2016-01-23 06:20:00程序设计版块每日发帖之星
日期:2016-01-31 06:20:00数据库技术版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-14 06:20:00
2 [报告]
发表于 2011-04-21 17:10 |只看该作者
/* ------------------------------------------------------------------------
*
* client_tipc.c
*
* Short description: TIPC hello world demo (client side)
* ------------------------------------------------------------------------
*
* Copyright (c) 2003, Ericsson Research Canada
* Copyright (c) 2010 Wind River Systems
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of Ericsson Research Canada nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* ------------------------------------------------------------------------
*/

#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>
#include <linux/tipc.h>

#define SERVER_TYPE  18888
#define SERVER_INST  17

void wait_for_server(__u32 name_type, __u32 name_instance, int wait)
{
        struct sockaddr_tipc topsrv;
        struct tipc_subscr subscr;
        struct tipc_event event;

        int sd = socket(AF_TIPC, SOCK_SEQPACKET, 0);

        memset(&topsrv, 0, sizeof(topsrv));
        topsrv.family = AF_TIPC;
        topsrv.addrtype = TIPC_ADDR_NAME;
        topsrv.addr.name.name.type = TIPC_TOP_SRV;
        topsrv.addr.name.name.instance = TIPC_TOP_SRV;

        /* Connect to topology server */

        if (0 > connect(sd, (struct sockaddr *)&topsrv, sizeof(topsrv))) {
                perror("Client: failed to connect to topology server");
                exit(1);
        }

        subscr.seq.type = htonl(name_type);
        subscr.seq.lower = htonl(name_instance);
        subscr.seq.upper = htonl(name_instance);
        subscr.timeout = htonl(wait);
        subscr.filter = htonl(TIPC_SUB_SERVICE);

        if (send(sd, &subscr, sizeof(subscr), 0) != sizeof(subscr)) {
                perror("Client: failed to send subscription");
                exit(1);
        }
        /* Now wait for the subscription to fire */

        if (recv(sd, &event, sizeof(event), 0) != sizeof(event)) {
                perror("Client: failed to receive event");
                exit(1);
        }
        if (event.event != htonl(TIPC_PUBLISHED)) {
                printf("Client: server {%u,%u} not published within %u [s]\n",
                       name_type, name_instance, wait/1000);
                exit(1);
        }

        close(sd);
}


int main(int argc, char *argv[], char *dummy[])
{
        int sd;
        struct sockaddr_tipc server_addr;
        char buf[40] = {"Hello World"};

        printf("****** TIPC client hello world program started ******\n\n");

        wait_for_server(SERVER_TYPE, SERVER_INST, 10000);

        sd = socket(AF_TIPC, SOCK_RDM, 0);

        server_addr.family = AF_TIPC;
        server_addr.addrtype = TIPC_ADDR_NAME;
        server_addr.addr.name.name.type = SERVER_TYPE;
        server_addr.addr.name.name.instance = SERVER_INST;
        server_addr.addr.name.domain = 0;

        if (0 > sendto(sd, buf, strlen(buf)+1, 0,
                       (struct sockaddr*)&server_addr, sizeof(server_addr))) {
                perror("Client: failed to send");
                exit(1);
        }

        if (0 >= recv(sd, buf, sizeof(buf), 0)) {
                perror("Client: unexpected response");
                exit(1);
        }

        printf("Client: received response: %s \n", buf);

        printf("\n****** TIPC client hello program finished ******\n");
        exit(0);
}

论坛徽章:
1
天蝎座
日期:2013-12-06 18:23:58
3 [报告]
发表于 2011-04-22 17:01 |只看该作者
回复 2# yjh777


    这个tipc现在应用的广吗? 比socket ipc 有啥优点吗? 求普及啊

论坛徽章:
84
每日论坛发贴之星
日期:2015-12-29 06:20:00每日论坛发贴之星
日期:2016-01-16 06:20:00每周论坛发贴之星
日期:2016-01-17 22:22:00程序设计版块每日发帖之星
日期:2016-01-20 06:20:00每日论坛发贴之星
日期:2016-01-20 06:20:00程序设计版块每日发帖之星
日期:2016-01-21 06:20:00每日论坛发贴之星
日期:2016-01-21 06:20:00程序设计版块每日发帖之星
日期:2016-01-23 06:20:00程序设计版块每日发帖之星
日期:2016-01-31 06:20:00数据库技术版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-16 06:20:00程序设计版块每日发帖之星
日期:2016-01-14 06:20:00
4 [报告]
发表于 2011-04-25 09:31 |只看该作者
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP