简体   繁体   English

在C中使用rpc将字符串作为参数传递

[英]pass string as argument using rpc in c

I am trying to write a simple RPC program that takes a string as argument and returns its length to the calling program. 我正在尝试编写一个简单的RPC程序,该程序将字符串作为参数并将其长度返回给调用程序。 I am using RPCGEN to generate the required stubs. 我正在使用RPCGEN生成所需的存根。 The code is as follows: The prog.x file: 代码如下:prog.x文件:

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

The client.c file: 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);

} }

and finally the serverproc.c file: 最后是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));
    }

The code compiles fine but on running it, it simply hangs and I have to use a Ctrl-C to end it. 代码可以很好地编译,但是在运行时,它只是挂起,我必须使用Ctrl-C结束它。 The problem probably lies in the serverproc.c file. 问题可能出在serverproc.c文件中。 What am I doing wrong? 我究竟做错了什么?

It may be due to following reasons : 可能是由于以下原因:

  • Check if either rpcbind or portmap is installed else do 检查是否安装了rpcbind或portmap,否则执行
      sudo apt-get install rpcbind 
  • If installed then do just one thing. 如果已安装,则只需做一件事。 : generate your client and server code using rpcgen only like follows : :仅使用rpcgen生成客户端和服务器代码,如下所示:

    rpcgen prog.x -a 

It will generate sample client and server code for you. 它将为您生成示例客户端和服务器代码。

  • Just add few lines in client code and in server code, set the calculated length to static variable "result", (already there). 只需在客户端代码和服务器代码中添加几行,即可将计算出的长度设置为静态变量“结果”(已经存在)。
  • Give it a try, it will work surely !! 试试看,它一定可以正常工作!

The hang is due to the fact that the server is listening for client connection. 挂起是由于服务器正在侦听客户端连接。 Using ctrl+c terminates the server. 使用ctrl + c会终止服务器。 This can be verified by adding a printf statement in serverproc.c and check if that gets printed in the server console whenever a client connection is tried upon. 可以通过在serverproc.c中添加printf语句并检查是否在尝试客户端连接时在服务器控制台中打印该语句来验证这一点。 Also If your client just seems to be hanging, chances are you have not started the portmapper. 另外,如果您的客户端似乎挂起,则可能是您尚未启动portmapper。

Commands to start portmapper 启动portmapper的命令

OS command Mac OS X 操作系统命令Mac OS X

launchctl start com.apple.portmap 

Linux 的Linux

/sbin/portmap

*BSD * BSD

/usr/sbin/rpcbind

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM