客户端给服务器端发送一个包含2给整数的字符串,服务器端发回他们的和。
1. radd.x
/* radd.x */ /* RPC declarations for add program */ /* http://zhoulifa.bokee.com/6129455.html */ /* ------------------------------------------------------------------ * RADDPROG -- remote program that provides Two Int Add * ------------------------------------------------------------------ */ program RADDPROG /* name of remote program ( not used ) */ { version RADDVERS /* declaration of version ( see below ) */ { int IntAdd ( string ) = 1; /* first procedure in this program */ } = 1; /* definition of the program version */ } = 0x30090949; /* remote program number ( must be unique ) */
上面代码的含义是,服务器端包含一个函数IntAdd,其参数为string,返回int。
2. 使用rpcgen生成所需文件
server端
rpcgen -Ss -o radd_srv_func.c radd.x
client端
rpcgen -Sc -o radd_client.c radd.x
Makefile文件
rpcgen -Sm radd.x > Makefile
3. 修改radd_srv_func.c,添加函数功能
#include "radd.h" int * intadd_1_svc(char **argp, struct svc_req *rqstp) { static int result; /* * insert server code here */ int num1, num2; sscanf(*argp, "%d %d", &num1, &num2); result = num1 + num2; return &result; }
4. 修改radd_client.c#include "radd.h" void raddprog_1(char *host) { CLIENT *clnt; int *result_1; char * intadd_1_arg; #ifndef DEBUG clnt = clnt_create (host, RADDPROG, RADDVERS, "udp"); if (clnt == NULL) { clnt_pcreateerror (host); exit (1); } #endif /* DEBUG */ char word[256]; while(1) { printf("Please input two integers: "); fgets(word, 256, stdin); intadd_1_arg = word; result_1 = intadd_1(&intadd_1_arg, clnt); if (result_1 == (int *) NULL) { clnt_perror (clnt, "call failed"); } printf("sum = %d\n", *result_1); } #ifndef DEBUG clnt_destroy (clnt); #endif /* DEBUG */ } int main (int argc, char *argv[]) { char *host; if (argc < 2) { printf ("usage: %s server_host\n", argv[0]); exit (1); } host = argv[1]; raddprog_1 (host); exit (0); }
5. 修改Makefile# This is a template Makefile generated by rpcgen # Parameters CLIENT = radd_client SERVER = radd_server SOURCES_CLNT.c = SOURCES_CLNT.h = SOURCES_SVC.c = SOURCES_SVC.h = SOURCES.x = radd.x TARGETS_SVC.c = radd_svc.c radd_srv_func.c TARGETS_CLNT.c = radd_clnt.c radd_client.c TARGETS = radd.h radd_clnt.c radd_svc.c OBJECTS_CLNT = $(SOURCES_CLNT.c:%.c=%.o) $(TARGETS_CLNT.c:%.c=%.o) OBJECTS_SVC = $(SOURCES_SVC.c:%.c=%.o) $(TARGETS_SVC.c:%.c=%.o) # Compiler flags CFLAGS += -g LDLIBS += -lnsl RPCGENFLAGS = # Targets all : $(CLIENT) $(SERVER) $(TARGETS) : $(SOURCES.x) rpcgen $(RPCGENFLAGS) $(SOURCES.x) $(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) $(TARGETS_CLNT.c) $(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) $(TARGETS_SVC.c) $(CLIENT) : $(OBJECTS_CLNT) $(LINK.c) -o $(CLIENT) $(OBJECTS_CLNT) $(LDLIBS) $(SERVER) : $(OBJECTS_SVC) $(LINK.c) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS) clean: $(RM) core $(TARGETS) $(OBJECTS_CLNT) $(OBJECTS_SVC) $(CLIENT) $(SERVER)
主要是这三行TARGETS_SVC.c = radd_svc.c radd_srv_func.c TARGETS_CLNT.c = radd_clnt.c radd_client.c TARGETS = radd.h radd_clnt.c radd_svc.c
6. make,然后即可运行生成的可执行文件运行server端,需要安装portmap,并执行portmap start命令。(开启端口映射功能)root@ubuntu:~/下载/RPC/add# ./radd_client 127.0.0.1 Please input two integers: 5 5 sum = 10 Please input two integers: 6 6 sum = 12