【问题标题】:pass string as argument using rpc in c在c中使用rpc将字符串作为参数传递
【发布时间】:2012-06-03 08:33:36
【问题描述】:

我正在尝试编写一个简单的 RPC 程序,它将字符串作为参数并将其长度返回给调用程序。我正在使用 RPCGEN 生成所需的存根。代码如下: prog.x 文件:

    program PROGRAM_PROG {
    version PROGRAM_VERS {
    int fun(string) = 1;    /* procedure number = 1 */
    } = 1;                          /* version number = 1 */
    } = 0x31234567;                     /* program number = 0x31234567 */

client.c 文件:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <rpc/rpc.h>    /* standard RPC include file */
    #include "prog.h"       /* this file is generated by rpcgen */
    #include "math.h"
    main(int argc, char *argv[])
    {
        CLIENT *cl;         /* RPC handle */
        char *server;
char *word;
int *lresult;      /* return value from bin_date_1() */

if (argc != 3) {
    fprintf(stderr, "usage: %s hostname\n", argv[0]);
    exit(1);
}

server = argv[1];
word = argv[2];

printf("%s======\n", server);
printf("%s======\n", word);
/*
 * Create client handle
 */

if ((cl = clnt_create(server, PROGRAM_PROG, PROGRAM_VERS, "udp")) == NULL) {
    /*
     * can't establish connection with server
     */
     clnt_pcreateerror(server);
     exit(2);
}

/*
 * First call the remote procedure "bin_date".
 */
printf("123\n");
if ( (lresult = fun_1(&word, cl)) == NULL) {
    clnt_perror(cl, server);
    exit(3);
}
printf("456\n");
printf("time on host %s = %d and look, word: %s\n",server, *lresult, word);


clnt_destroy(cl);         /* done with the handle */
exit(0);

}

最后是 serverproc.c 文件:

    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    #include <stdlib.h>
    #include <rpc/rpc.h>
    #include "prog.h"


    int *fun_1(char **p, CLIENT *cl)
    {
    int len;
    int t;
    t = strlen(*p);

    return &t;
    }

    int * fun_1_svc(char **p, struct svc_req *cl){
    CLIENT *client;
    return(fun_1(0, client));
    }

代码编译得很好,但是在运行它时,它只是挂起,我必须使用 Ctrl-C 来结束它。问题可能出在 serverproc.c 文件中。我究竟做错了什么?

【问题讨论】:

    标签: c rpc


    【解决方案1】:

    这可能是由于以下原因:

    • 检查是否安装了 rpcbind 或 portmap
       sudo apt-get install rpcbind
    • 如果已安装,则只做一件事。 : 仅使用 rpcgen 生成您的客户端和服务器代码,如下所示:

         rpcgen prog.x -a 

    它将为您生成示例客户端和服务器代码。

    • 只需在客户端代码和服务器代码中添加几行,将计算的长度设置为静态变量“result”,(已经存在)。
    • 试一试,肯定能用!!

    【讨论】:

      【解决方案2】:

      挂起是由于服务器正在侦听客户端连接。使用 ctrl+c 终止服务器。这可以通过在 serverproc.c 中添加 printf 语句来验证,并检查是否在尝试客户端连接时在服务器控制台中打印。此外,如果您的客户端似乎只是挂起,那么您可能还没有启动端口映射器。

      启动端口映射器的命令

      操作系统命令 Mac OS X

      launchctl start com.apple.portmap 
      

      Linux

      /sbin/portmap
      

      *BSD

      /usr/sbin/rpcbind
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-31
        相关资源
        最近更新 更多