系统:LinuxMint mate 17.2 32bit
gcc:gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04)
rpcgen:rpcgen (Ubuntu EGLIBC 2.19-0ubuntu6.6) 2.19
rpc依赖库 portmap或者 rpcbind
可执行 apt-get install portmap 进行安装,准备完毕后,详见以下步骤
创建规格文件 math.x,内容如下
/* filename: math.x */
const ADD = 0;
const SUB = 1;
const MUL = 2;
const DIV = 3;
struct MATH
{
int op; /* 0-ADD, 1-SUB, 2-MUL, 3-DIV */
float arg1;
float arg2;
float result;
};
program MATH_PROG
{
version MATH_VER
{
struct MATH MATH_PROC(struct MATH) = 1;
} = 2;
} = 0x20000001;
终端中运行
rpcgen –C –a math.x
生成7个文件,math.h、math_xdr.c、math_svc.c、math_clnt.c、Makefile.math、math_client.c、math_server.c
分别修改 math_server.c 和math_client.c如下
math_client.c
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/
#include "math.h"
void math_prog_2(char *host) {
CLIENT *clnt;
struct MATH *result_1;
struct MATH math_proc_2_arg;
/* 2006/07/04 Dongyy Add -> */
char c;
printf(
"choose the operation:\n\t0---ADD\n\t1---SUB\n\t2---MUL\n\t3---DIV\n");
c = getchar();
switch (c) {
case '0':
math_proc_2_arg.op = ADD;
break;
case '1':
math_proc_2_arg.op = SUB;
break;
case '2':
math_proc_2_arg.op = MUL;
break;
case '3':
math_proc_2_arg.op = DIV;
break;
default:
printf("error:operate\n");
exit(1);
}
printf("input the first number:");
scanf("%f", &math_proc_2_arg.arg1);
printf("input the second number:");
scanf("%f", &math_proc_2_arg.arg2);
/* <- 2006/07/04 Dongyy Add */
#ifndef DEBUG
clnt = clnt_create(host, MATH_PROG, MATH_VER, "udp");
if (clnt == NULL) {
clnt_pcreateerror(host);
exit(1);
}
#endif /* DEBUG */
result_1 = math_proc_2(&math_proc_2_arg, clnt);
if (result_1 == (struct MATH *) NULL) {
clnt_perror(clnt, "call failed");
}
#ifndef DEBUG
clnt_destroy(clnt);
#endif /* DEBUG */
/* 2006/07/04 Dongyy Add -> */
printf("The Result is %.3f \n", result_1->result);
/* <- 2006/07/04 Dongyy Add */
}
int main(int argc, char *argv[]) {
char *host;
if (argc < 2) {
printf("usage: %s server_host\n", argv[0]);
exit(1);
}
host = argv[1];
math_prog_2(host);
exit(0);
}
math_server.c
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/
#include "math.h"
struct MATH *
math_proc_2_svc(struct MATH *argp, struct svc_req *rqstp) {
static struct MATH result;
/*
* insert server code here
*/
/* 2006/07/04 Dongyy Add -> */
switch (argp->op) {
case ADD:
result.result = argp->arg1 + argp->arg2;
break;
case SUB:
result.result = argp->arg1 - argp->arg2;
break;
case MUL:
result.result = argp->arg1 * argp->arg2;
break;
case DIV:
result.result = argp->arg1 / argp->arg2;
break;
default:
break;
}
/* <- 2006/07/04 Dongyy Add */
return &result;
}
编译,测试
gcc -Wall -o math_server math_server.c math_svc.c math_xdr.c
gcc -Wall -o math_client math_client.c math_xdr.c math_clnt.c
执行./math_server
另起终端执行: ./math_client 127.0.0.1
如下图
各位看官,自行脑补代码实现去。。。。
本文介绍了在Linux Mint 17.2上使用C语言进行RPC远程调用的步骤。首先确保安装了rpc依赖库portmap,然后通过rpcgen工具从规格文件math.x生成相关源码。接着修改math_server.c和math_client.c,分别编译生成服务器和客户端程序,并演示了如何运行和测试。最后鼓励读者自己尝试实现代码。
613

被折叠的 条评论
为什么被折叠?



