RPC编程
一.概述
一般的程序只能在本地编译执行,主程序和被调程序也是本地调用关系。RemoteProcedureCalls(RPC)可以实现远程调用,客户端程序可以调用远端服务器的过程,服务器中的过程执行操作,将执行结果返回给客户端。
二.RPC开发过程
RPC开发一共三步:
第一.定义client和server交互接口,即*.x文件。
第二.开发client.c客户端程序。
第三.开发server.c服务端程序
以下面这个本地调用程序为例,讲述如何进行RPC开发。
/*client1.c*/
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
long bin_date(void);
char *str_date(long bintime);
int main(int argc, char *argv[]){
long lresult; /*return from bin_date*/
char *sresult; /*return from str_date*/
if(argc!=1){
fprintf(stderr,"usage:%s\n",argv[0]);
exit(1);
}
/*call the procedure bin_date*/
lresult=bin_date();
printf("time is %ld\n",lresult);
/*convert the result to a date string*/
sresult=str_date(lresult);
printf("date is %s",sresult);
exit(0);
}
/*return system date as # seconds since Jan 1 1970 0:00, in binary formate*/
long bin_date(void){
long timeval;
long time(); /*unix time function, returns time*/
timeval=time((long *)0);
return timeval;
}
/*str_date convert a binary time into a date sting, return the date string*/
char * str_date(long bintime){
char *ptr;
char *ctime();/*unix library funciton that dose the work*/
ptr=ctime(&bintime);
return ptr;
}
以上所以在本地运行程序,下面想通过RPC在远端服务器上调用上面两个函数。
1.交互协议接口
1.1定义client和server接口,*.x文件
一个远程调用过程由三个要素决定:程序号(