在c中使用rpc将字符串作为参数传递
我正在尝试编写一个简单的RPC程序,它将字符串作为参数,并将其长度返回给调用程序。我正在使用RPCGEN生成所需的存根。代码如下: prog.x文件:在c中使用rpc将字符串作为参数传递,c,rpc,C,Rpc,我正在尝试编写一个简单的RPC程序,它将字符串作为参数,并将其长度返回给调用程序。我正在使用RPCGEN生成所需的存根。代码如下: prog.x文件: program PROGRAM_PROG { version PROGRAM_VERS { int fun(string) = 1; /* procedure number = 1 */ } = 1; /* version number = 1 */
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);
#包括
#包括
#包括
#include/*标准RPC包含文件*/
#包含“prog.h”/*此文件由rpcgen生成*/
#包括“math.h”
main(int argc,char*argv[])
{
客户端*cl;/*RPC句柄*/
字符*服务器;
字符*字;
int*lresult;/*从bin_date_1()返回的值*/
如果(argc!=3){
fprintf(stderr,“用法:%s主机名\n”,argv[0]);
出口(1);
}
服务器=argv[1];
word=argv[2];
printf(“%s=====\n”,服务器);
printf(“%s====\n”,word);
/*
*创建客户端句柄
*/
if((cl=clnt\u create(服务器、程序、程序版本,“udp”))==NULL){
/*
*无法与服务器建立连接
*/
clnt_pcreateerror(服务器);
出口(2);
}
/*
*首先调用远程过程“bin_date”。
*/
printf(“123\n”);
if((lresult=fun_1(&word,cl))==NULL){
clnt_perror(cl,服务器);
出口(3);
}
printf(“456\n”);
printf(“主机%s上的时间=%d,并查看,单词:%s\n”,服务器,*lresult,单词);
clnt_销毁(cl);/*用手柄完成*/
出口(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));
}
#包括
#包括
#包括
#包括
#包括
#包括“程序h”
int*fun_1(字符**p,客户端*cl)
{
内伦;
int t;
t=strlen(*p);
返回&t;
}
int*fun\u 1\u svc(字符**p,结构svc\u请求*cl){
客户*客户;
返回(fun_1(0,客户));
}
代码编译得很好,但在运行它时,它只是挂起,我必须使用Ctrl-C来结束它。问题可能出在serverproc.c文件中。我做错了什么 可能是由于以下原因:
- 检查是否安装了rpcbind或portmap,否则执行以下操作
sudo apt-get install rpcbind
sudo apt get install rpcbind
- 如果已安装,则只需做一件事:使用rpcgen生成客户端和服务器代码,如下所示:
rpcgen prog.x-a
它将为您生成示例客户端和服务器代码
- 只需在客户端代码和服务器代码中添加几行代码,将计算的长度设置为静态变量“result”(已经存在)
- 试试看,它肯定会有用的李>
launchctl start com.apple.portmap
Linux
*BSD
/usr/sbin/rpcbind
/sbin/portmap
/usr/sbin/rpcbind