首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在c++中使用rpc将字符串作为参数传递

问在c++中使用rpc将字符串作为参数传递
EN

Stack Overflow用户
提问于 2012-05-29 13:53:08
回答 1查看 3.6K关注 0票数 1

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

代码语言:javascript
复制
    program PROGRAM_PROG {
    version PROGRAM_VERS {
    int fun(string) = 1;    /* procedure number = 1 */
    } = 1;                          /* version number = 1 */
    } = 0x31234567;                     /* program number = 0x31234567 */

client.c文件:

代码语言:javascript
复制
    #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文件:

代码语言:javascript
复制
    #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文件中。我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2015-03-01 05:32:29

挂起的原因是服务器正在侦听客户端连接。使用ctrl+c会终止服务器。可以通过在serverproc.c中添加printf语句来验证这一点,并检查在尝试客户端连接时是否在服务器控制台中打印该语句。另外,如果您的客户端似乎处于挂起状态,则很可能您尚未启动端口映射器。

启动端口映射程序的命令

OS命令Mac OS X

代码语言:javascript
复制
launchctl start com.apple.portmap 

Linux

代码语言:javascript
复制
/sbin/portmap

*BSD

代码语言:javascript
复制
/usr/sbin/rpcbind
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10793727

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档