日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

Sun RPC 編程簡介( 三 )


#include
#include /* always needed */
#include "time.h" /* time.h will be generated by rpcgen */
#include
/* 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, "%sn", *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
#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 messagen", 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");

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 timen",argv[0]);
exit(1);
}
printf("From the Time Server ...%sn",result);
clnt_destroy( clnt );
exit(0);
}
有了以上的三段代碼后,就可用rpcgen 編譯工具進行RPC協(xié)議編譯,命令如下:
$rpcgen time.x
rpcgen 會自動生成time.h、time_svc.c、time_clnt.c
再用系統(tǒng)提供的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時鐘),筆者曾將此程序應用于計費服務器同時鐘服務器同步監(jiān)測的網管
系統(tǒng)中,運行穩(wěn)定,獲得了較好的效果 。應該說RPC的應用是十分廣泛的,特別是
在分布式計算領域中尤為顯得重要 。當然,筆者也是剛接觸RPC,還有很多地方了
解的不夠深刻,望廣大讀者多指教 。



參考文獻:
《SUN Solaris8 ONCDev》

推薦閱讀