Linux中国 Linux中国门户站!
设为主页 设为主页
收藏本站 收藏本站
 
当前位置 :首页 ->操作系统 ->Solaris ->正文

Sun RPC 编程简介

来源:Linuxdby.com 作者:Webmaster 时间:2007-05-26 点击: [收藏] [投稿]


  if (clnt == (CLIENT *)NULL) {
   /*
    * Couldn't establish connection
    * with server.
    * Print error message and die.
    */

   clnt_pcreateerror(server);
   exit(1);
   }

  /*
   
* Call the remote procedure
   * "printime" on the server
   */

   result =*printime_1(&message,clnt);
   if (result== (char *)NULL) {
     /*
      * An error occurred while calling
      * the server.
      * Print error message and die.
      */

     clnt_perror(clnt, server);
     exit(1);
    }

   /* Okay, we successfully called
    * the remote procedure.
    */

   if (strcmp(result,"Error") == 0) {

   /*
    * Server was unable to print
    * the time.
    * Print error message and die.
    */

    fprintf(stderr, "%s: could not get the time\n",argv[0]);
    exit(1);
    }
    printf("From the Time Server ...%s\n",result);  
    clnt_destroy( clnt );
    exit(0);
}
   有了以上的三段代码后,就可用rpcgen 编译工具进行RPC协议编译,命令如下:
   $rpcgen time.x
   rpcgen 会自动生成time.h、time_svc.c、time_clnt.c
   再用系统提供的gcc进行C的编译,命令如下:
   $gcc rtime.c time_clnt.c -o rtime -lnsl //客户端编译
$gcc time_proc.c time_svc.c -o time_server -lnsl //服务器端编译
编译成功后即可在Server端运行time_server,立即将该服务绑定在rpc服务端口上提供
服务。在客户端运行./rdate hostname msg (msg 是一字符串,笔者用来测试时建立的),
立即会返回hostname 端的时间。
     由于,在Sun Solaris 中无法获取远端Server 上时钟信息的功能(不改变本
     地Server时钟),笔者曾将此程序应用于计费服务器同时钟服务器同步监测的网管
     系统中,运行稳定,获得了较好的效果。应该说RPC的应用是十分广泛的,特别是
     在分布式计算领域中尤为显得重要。当然,笔者也是刚接触RPC,还有很多地方了
     解的不够深刻,望广大读者多指教。



参考文献:
《SUN Solaris8 ONC+ Dev》


 如果您对本文有任何疑问或者建议,请到讨论区发表您的意见: >> 论坛入口 <<



上一篇:如何以Solaris架设FTP虚拟系统   下一篇:solaris8学习资料 - 第一课

文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【我要投稿】 【论坛讨论
更多相关文章
Power by linux-cn.com 粤ICP备05006655号