Sun RPC 编程简介Svc_tli_create( ) 在指定的传输端口上建立服务句柄 Rpcb_set( ) 通过调用rpcbind将RPC服务和网络地址做映射 Rpcb_unset( ) 删除rpcb_set( ) 所建的映射关系 Rpcb_getaddr( ) 调用rpcbind来犯会指定RPC服务所对应的传输地址 Svc_reg( ) 将指定的程序和版本号与相应的时间例程建起关联 Svc_ureg( ) 删除有svc_reg( ) 所建的关联 Clnt_call( ) 客户端向指定的服务器端发起RPC请求 5、 底层例程 该层提供了所有对传输选项进行控制的调用接口,它提供了如下功能函数。 函数名 功能描述 Clnt_dg_create( ) 采用无连接方式向远程过程在客户端建立客户句柄 Svc_dg_create( ) 采用无连接方式建立服务句柄 Clnt_vc_create( ) 采用面向连接的方式建立客户句柄 Svc_vc_create( ) 采用面向连接的方式建立RPC服务句柄 Clnt_call( ) 客户端向服务器端发送调用请求 四、 实例介绍 以下我将通过实例向读者介绍通过简单层RPC的实现方法。通常在此过程中我们 将使用RPC协议编译工具-Rpcgen。Rpcgen 工具用来生成远程程序接口模块,它将以RPC 语言书写的源代码进行编译,Rpc 语言在结构和语法上同C语言相似。由Rpcgen 编译生 成的C源程序可以直接用C编译器进行编译,因此整个编译工作将分为两个部分。Rpcgen 的源程序以.x结尾,通过其编译将生成如下文件: a) 一个头文件(.h)包括服务器和客户端程序变量、常量、类型等说明。 b) 一系列的XDR例程,它可以对头文件中定义的数据类型进行处理。 c) 一个Server 端的标准程序框架。 d) 一个Client 端的标准程序框架。 当然,这些输出可以是选择性的,Rpcgen 的编译选项说明如下: 选项 功能 '-' a 生成所有的模板文件 '-' Sc 生成客户端的模板文件 '-' Ss 生成服务器端的模板文件 '-' Sm 生成Makefile 文件 (详见Solaris Rpcgen Manaul) Rpcgen 源程序 time.x: /* time.x: Remote time printing protocol */ program TIMEPROG { version PRINTIMEVERS { string PRINTIME(string) = 1; } = 1; } = 0x20000001; time_proc.c源程序: /* time_proc.c: implementation of the remote procedure "printime" */ #include <stdio.h> #include <rpc/rpc.h> /* always needed */ #include "time.h" /* time.h will be generated by rpcgen */ #include <time.h> /* Remote version of "printime" */ char ** printime_1(char **msg,struct svc_req *req) { static char * result; /* must be static! */ static char tmp_char[100]; time_t rawtime; FILE *f; f = fopen("/tmp/rpc_result", "a+"); if (f == (FILE *)NULL) { strcpy(tmp_char,"Error"); result = tmp_char;; return (&result); } fprintf(f, "%s\n", *msg); //used for debugging fclose(f); time(&rawtime); sprintf(tmp_char,"Current time is :%s",ctime(&rawtime)); result =tmp_char; return (&result); } rtime.c源代码 /* * rtime.c: remote version * of "printime.c" */ #include <stdio.h> #include "time.h" /* time.h generated by rpcgen */ main(int argc, char **argv) { CLIENT *clnt; char *result; char *server; char *message; if (argc != 3) { fprintf(stderr, "usage: %s host message\n", argv[0]); exit(1); } server = argv[1]; message = argv[2]; /* * Create client "handle" used for * calling TIMEPROG on the server * designated on the command line. */ clnt = clnt_create(server, TIMEPROG, PRINTIMEVERS, "visible"); 上一篇:如何以Solaris架设FTP虚拟系统 下一篇:solaris8学习资料 - 第一课 更多相关文章
|
推荐文章
精彩文章
|