zoukankan      html  css  js  c++  java
  • RPC的应用(The lowest layer of RPC)

    server端代码:

    #include <stdio.h>
    #include <rpc/rpc.h>
    #include <rpcsvc/rusers.h>

    void nuser(struct svc_req * rqstp, SVCXPRT * transp)
    {
        unsigned  nusers;
        unsigned input;
        switch (rqstp->rq_proc) {
            case NULLPROC:
                if (!svc_sendreply(transp, xdr_void, 0)) {
                    fprintf(stderr, "can't reply to RPC call ");
                    exit(1);
                }
                return;
            case RUSERSPROC_NUM:
        /* Code here to compute the number of users and
        * assign to the variable nusers
        */
                if(!svc_getargs(transp,xdr_int,&input))
                {
                    svcerr_decode(transp);
                    return ;
                }
                fprintf(stderr,"This is the lowest layer of RPC!! and recieve data is %u ",input);
                nusers = (unsigned)10 + input;
            if (!svc_sendreply(transp, xdr_u_long, &nusers)) {
                fprintf(stderr, "can't reply to RPC call ");
                exit(1);
            }
            return;
            default:
                svcerr_noproc(transp);
            return;
        }
    }


    int main(void)
    {
        SVCXPRT *transp;
        void nuser();
        transp = svcudp_create(RPC_ANYSOCK);
        if (transp == NULL){
            fprintf(stderr, "can't create an RPC server ");
            exit(1);
        }
        pmap_unset(RUSERSPROG, RUSERSVERS);
        if (!svc_register(transp, RUSERSPROG, RUSERSVERS, nuser,
            IPPROTO_UDP)) {
                fprintf(stderr, "can't register RUSER service ");
                exit(1);
        }
        svc_run(); /* never returns */
        fprintf(stderr, "should never reach this point ");
        exit(1);
    }

    client端代码:

    /*
    * howmany3.c
    */
    #include <stdio.h>
    #include <rpc/rpc.h>
    #include <rpcsvc/rusers.h>
    #include <sys/socket.h>
    #include <sys/time.h>
    #include <netdb.h>


    main(int argc, char **argv)
    {
        struct hostent *hp;
        struct timeval pertry_timeout, total_timeout;
        struct sockaddr_in server_addr;
        int sock = RPC_ANYSOCK;
        register CLIENT *client;
        enum clnt_stat clnt_stat;
        unsigned nusers;
        unsigned input = 10;
        if (argc != 2) {
            fprintf(stderr, "usage: howmany3 hostname ");
            exit(1);
        }
        if ((hp = gethostbyname(argv[1])) == NULL) {
            herror(argv[1]);
            exit(1);
        }
        pertry_timeout.tv_sec = 3;
        pertry_timeout.tv_usec = 0;
        bcopy(hp->h_addr, (caddr_t)&server_addr.sin_addr,
        hp->h_length);
        server_addr.sin_family = AF_INET;
        server_addr.sin_port = 0;
        if ((client = clntudp_create(&server_addr, RUSERSPROG,
            RUSERSVERS, pertry_timeout, &sock)) == NULL) {
                clnt_pcreateerror("clntudp_create");
                exit(1);
        }
        total_timeout.tv_sec = 20;
        total_timeout.tv_usec = 0;
        
        clnt_stat = clnt_call(client,RUSERSPROC_NUM,xdr_u_int,&input,
                        xdr_u_int,&nusers, total_timeout);
        if (clnt_stat != RPC_SUCCESS) {
            clnt_perror(client, "rpc");
            exit(1);
        }
        printf("%d users on %s ", nusers, argv[1]);
        
        clnt_destroy(client);
        close(sock);
        exit(0);
    }

  • 相关阅读:
    python中的递归算法
    python中的闭包函数
    python中的函数对象和函数嵌套
    python中函数与参数的简介
    python中的文件操作: 包括文件的打开方式和文件的修改
    python中元组;字典;集合的内置方法以及它们的取值方式
    python中的数字类型及内置方式;字符类型及内置方式和列表类型及内置方法
    python中的while和for循环以及break,continue结束语句。
    判断字符串以字母开头,后面可以是数字,下划线,字母,长度为6-30
    Prompt box是什么?它的返回值有什么用?
  • 原文地址:https://www.cnblogs.com/linghuchong0605/p/3654693.html
Copyright © 2011-2022 走看看