rpcgen的簡單講解及例子程式

2021-06-18 05:34:59 字數 3277 閱讀 3248

rpcgen

簡介

rpcgen可以自動生成rpc伺服器程式的大多數**,它的輸入為乙個規格說明檔案,它的輸出為乙個c語言的源程式。規格檔案(*.x)包含常量、全域性資料型別以及遠端過程的宣告。rpcgen產生的**包含了實現客戶機和伺服器程式所需要的大部分源**。他包括引數整理、傳送rpc報文、引數和結果的外部資料表示以及本地資料表示的轉換等。不過在由rpcgen生成的原始檔中,沒有過程的具體實現,所以程式設計師必須要手工編輯這些檔案,實現這些過程。

rpc

自動生成的檔案

檔名 作用

makefile.file

該檔案用於編譯所有客戶機,伺服器**

file_clnt.c

該檔案包含client_stub,程式設計師一般不用修改

file_svc.c

該檔案包含server_stub,程式設計師一般不用修改

file.h

該檔案包含了從說明中產生的所有xdr型別

file_xdr.c

該檔案包含了客戶機和伺服器stub所需的xdr過濾器,程式設計師一般不用修改

file_server.c

如果生成此檔案,則該檔案包含遠端服務的stub

file_client.c

如果生成此檔案,則該檔案包含了骨架客戶機程式。

rpcgen

的部分選項

-a 生成所有源程式,包括客戶機和伺服器源程式。

-c 使用ansi c標準生成編碼。

-c 生成xdr轉碼c程式。(file_xdr.c)。

-l 生成客戶機stubs。(file_clnt.c)

-m 生成伺服器stubs,但是不生成main函式。(file_svc.c)

-s  rpcgen –c –s tcp file.x,生成伺服器stubs,用tcp協議,同時生成了main函式。(file_svc.c)

-h 生成標頭檔案。

-sc 生成骨架客戶機程式,(file_client.c),生成後還需要手動新增**。

-ss 生成伺服器程式,(file_server.c),生成後還需要手動新增**。

rpcgen –c  file.x 生成file_xdr.c,file.h,makefile.file,file_svc.c和file_client.c

rpcgen –c –a file.x 比上面多生成了2個檔案,file_server.c和file_client.c

rpcgen

示例程式

規格檔案(math.x)

/* filename: math.x */

const add = 0;

const sub = 1;

const mul = 2;

const div = 3;

struct math ;

program math_prog

= 2;

} = 0x20000001;

用rpcgen –c –a math.x生成7個檔案,math.h、math_xdr.c、math_svc.c、math_clnt.c、makefile.math、math_client.c、math_server.c。

在math_client.c中新增**,下面是新增後的**:/*

* this is sample code generated by rpcgen.

* these are only templates and you can use them

* as a guideline for developing your own functions. */

#include "math.h"

void math_prog_2(char *host)

printf("input the first number:");

scanf("%f", &math_proc_2_arg.arg1);

printf("input the second number:");

scanf("%f", &math_proc_2_arg.arg2);

/* <- 2006/07/04 add */

#ifndef   debug

clnt = clnt_create (host, math_prog, math_ver, "udp");

if (clnt == null)

#endif    /* debug */

result_1 = math_proc_2(&math_proc_2_arg, clnt);

if (result_1 == (struct math *) null)

#ifndef   debug

clnt_destroy (clnt);

#endif    /* debug */

/* 2006/07/04 add -> */

printf("the result is %.3f /n", result_1->result);

/* <- 2006/07/04 add */}

int main (int argc, char *argv)

host = argv[1];

math_prog_2 (host);

exit (0); }

在math_server.c中新增**,下面是新增後的**:/*

* this is sample code generated by rpcgen.

* these are only templates and you can use them

* as a guideline for developing your own functions. */

#include "math.h"

struct math *

math_proc_2_svc(struct math *argp, struct svc_req *rqstp)

/* <- 2006/07/04 add */

return &result; }

紅色字型部分為用rpcgen生成**後,手動新增的**。

新增完後,執行make –f makefile.math編譯生成math_client和math_server,在命令列執行math_server &,然後執行math_client 127.0.0.1,按照提示輸入內容就ok了。

參考書籍《linux c 高階程式設計師指南》

rpcgen的簡單講解及例子程式

rpcgen 簡介 rpcgen 可以自動生成 rpc伺服器程式的大多數 它的輸入為乙個規格說明檔案,它的輸出為乙個 c語言的源程式。規格檔案 x 包含常量 全域性資料型別以及遠端過程的宣告。rpcgen 產生的 包含了實現客戶機和伺服器程式所需要的大部分源 他包括引數整理 傳送 rpc報文 引數和...

rpcgen的簡單講解及例子程式

rpcgen可以自動生成rpc伺服器程式的大多數 它的輸入為乙個規格說明檔案,它的輸出為乙個c語言的源程式。規格檔案 x 包含常量 全域性資料型別以及遠端過程的宣告。rpcgen產生的 包含了實現客戶機和伺服器程式所需要的大部分源 他包括引數整理 傳送rpc報文 引數和結果的外部資料表示以及本地資料...

android shape講解小全及例子

原文 例子 shape xmlns android android shape rectangle oval line ring 預設為rectangle corners shape rectangle 時使用,android radius integer 半徑,會被下邊的屬性覆蓋,預設為1dp,a...