Ò»¡¢¸ÅÊö
ÔÚ´«Í³µÄ±à³Ì¸ÅÄîÖУ¬¹ý³ÌÊÇÓɳÌÐòÔ±ÔÚ±¾µØ±àÒëÍê³É£¬²¢Ö»ÄܾÖÏÞÔÚ±¾µØÔËÐеÄÒ»¶Î´úÂ룬Ҳ¼´ÆäÖ÷³ÌÐòºÍ¹ý³ÌÖ®¼äµÄÔËÐйØ
ϵÊDZ¾µØµ÷ÓùØÏµ¡£Òò´ËÕâÖֽṹÔÚÍøÂçÈÕÒæ·¢Õ¹µÄ½ñÌìÒÑÎÞ·¨ÊÊӦʵ¼ÊÐèÇó¡£×ܶøÑÔÖ®£¬´«Í³¹ý³Ìµ÷ÓÃģʽÎÞ·¨³ä·ÖÀûÓÃÍøÂçÉÏÆäËûÖ÷»úµÄ×ÊÔ´£¨ÈçCPU¡¢
MemoryµÈ£©£¬Ò²ÎÞ·¨Ìá¸ß´úÂëÔÚʵÌå¼äµÄ¹²Ïí³Ì¶È£¬Ê¹µÃÖ÷»ú×ÊÔ´´óÁ¿ÀË·Ñ¡£
¶ø±¾ÎÄÒª½éÉܵÄRPC±à³Ì£¬ÕýÊǺܺõؽâ¾öÁË´«Í³¹ý³ÌËù´æÔÚµÄһϵÁб׶ˡ£Í¨¹ýRPCÎÒÃÇ¿ÉÒÔ³ä·ÖÀûÓ÷ǹ²ÏíÄÚ´æµÄ¶à´¦
ÀíÆ÷»·¾³£¨ÀýÈçͨ¹ý¾ÖÓòÍøÁ¬½ÓµÄ¶ą̀¹¤×÷Õ¾£©,ÕâÑù¿ÉÒÔ¼ò±ãµØ½«ÄãµÄÓ¦Ó÷ֲ¼ÔÚ¶ą̀¹¤×÷Õ¾ÉÏ£¬Ó¦ÓóÌÐò¾ÍÏñÔËÐÐÔÚÒ»¸ö¶à´¦ÀíÆ÷µÄ¼ÆËã»úÉÏÒ»Ñù¡£Äã¿ÉÒÔ·½±ã
µÄʵÏÖ¹ý³Ì´úÂë¹²Ïí£¬Ìá¸ßϵͳ×ÊÔ´µÄÀûÓÃÂÊ£¬Ò²¿ÉÒÔ½«ÒÔ´óÁ¿ÊýÖµ´¦ÀíµÄ²Ù×÷·ÅÔÚ´¦ÀíÄÜÁ¦½ÏÇ¿µÄϵͳÉÏÔËÐУ¬´Ó¶ø¼õÇáǰ¶Ë»úµÄ¸ºµ£¡£
¶þ¡¢RPCµÄ½á¹¹ÔÀí¼°Æäµ÷ÓûúÖÆ
ÈçǰËùÊöRPCÆäʵҲÊÇÒ»ÖÖC/SµÄ±à³Ìģʽ£¬ÓеãÀàËÆC/S Socket
±à³Ìģʽ£¬µ«Òª±ÈËü¸ü¸ßÒ»²ã¡£µ±ÎÒÃÇÔÚ½¨Á¢RPC·þÎñÒԺ󣬿ͻ§¶ËµÄµ÷ÓòÎÊýͨ¹ýµ×²ãµÄRPC´«ÊäͨµÀ£¬¿ÉÒÔÊÇUDP£¬Ò²¿ÉÒÔÊÇTCP£¨Ò²¼´TI-RPC
¡ªÎÞ¹ØÐÔ´«Ê䣩£¬²¢¸ù¾Ý´«ÊäǰËùÌṩµÄÄ¿µÄµØÖ·¼°RPCÉϲãÓ¦ÓóÌÐòºÅתÖÁÏàÓ¦µÄRPC Application
Porgramme Server £¬ÇÒ´ËʱµÄ¿Í»§¶Ë´¦Óڵȴý״̬£¬Ö±ÖÁÊÕµ½Ó¦´ð»òTime Out³¬Ê±Ðźš£¾ßÌåµÄÁ÷³ÌͼÈçͼ1¡£µ±·þÎñÆ÷¶Ë»ñµÃÁËÇëÇóÏûÏ¢£¬Ôò»á¸ù¾Ý×¢²áRPCʱ¸æËßRPCϵͳµÄÀý³ÌÈë¿ÚµØÖ·£¬Ö´ÐÐÏàÓ¦µÄ²Ù×÷£¬²¢½«½á¹û·µ»ØÖÁ¿Í
»§¶Ë¡£µ±Ò»´ÎRPCµ÷ÓýáÊøºó£¬ÏàÓ¦Ï̷߳¢ËÍÏàÓ¦µÄÐźţ¬¿Í»§¶Ë³ÌÐò²Å»á¼ÌÐøÔËÐС£
µ±È»£¬Ò»Ì¨·þÎñÖ÷»úÉÏ¿ÉÒÔÓжà¸öÔ¶³Ì¹ý³ÌÌṩ·þÎñ£¬ÄÇôÈçºÎÀ´±íʾһ¸öΨһ´æÔÚµÄÔ¶³Ì¹ý³ÌÄØ£¿Ò»¸öÔ¶³Ì¹ý³ÌÊÇÓÐÈý¸öÒªËØ
À´Î¨Ò»È·¶¨µÄ£º³ÌÐòºÅ¡¢°æ±¾ºÅºÍ¹ý³ÌºÅ¡£³ÌÐòºÅÊÇÓÃÀ´Çø±ðÒ»×éÏà¹ØµÄ²¢ÇÒ¾ßÓÐΨһ¹ý³ÌºÅµÄÔ¶³Ì¹ý³Ì¡£Ò»¸ö³ÌÐò¿ÉÒÔÓÐÒ»¸ö»ò¼¸¸ö²»Í¬µÄ°æ±¾£¬¶øÃ¿¸ö°æ±¾µÄ³Ì
Ðò¶¼°üº¬Ò»ÏµÁÐÄܱ»Ô¶³Ìµ÷ÓõĹý³Ì£¬Í¨¹ý°æ±¾µÄÒýÈ룬ʹµÃ²»Í¬°æ±¾ÏµÄRPCÄÜͬʱÌṩ·þÎñ¡£Ã¿¸ö°æ±¾¶¼°üº¬ÓÐÐí¶à¿É¹©Ô¶³Ìµ÷ÓõĹý³Ì£¬Ã¿¸ö¹ý³ÌÔòÓÐÆäΨ
Ò»±êʾµÄ¹ý³ÌºÅ¡£
Èý¡¢»ùÓÚRPCµÄÓ¦ÓÃϵͳ¿ª·¢
ͨ¹ýÒÔÉ϶ÔRPCÔÀíµÄ¼ò½éºó£¬ÎÒÃÇÔÙÀ´¼ÌÐøÌÖÂÛÈçºÎÀ´¿ª·¢»ùÓÚRPCµÄÓ¦ÓÃϵͳ¡£Ò»°ã¶øÑÔÔÚ¿ª·¢RPCʱ£¬ÎÒÃÇͨ³£·ÖΪÈý¸ö²½Ö裺
a¡¢¶¨Òå˵Ã÷¿Í»§/·þÎñÆ÷µÄͨÐÅÐÒé¡£
ÕâÀïËù˵µÄͨÐÅÐÒéÊÇÖ¸¶¨Òå·þÎñ¹ý³ÌµÄÃû³Æ¡¢µ÷ÓòÎÊýµÄÊý¾ÝÀàÐͺͷµ»Ø²ÎÊýµÄÊý¾ÝÀàÐÍ£¬»¹°üÀ¨µ×²ã´«ÊäÀàÐÍ£¨¿ÉÒÔÊÇ
UDP»òTCP£©£¬µ±È»Ò²¿ÉÒÔÓÉRPCµ×²ãº¯Êý×Ô¶¯Ñ¡ÔñÁ¬½ÓÀàÐͽ¨Á¢TI-RPC¡£×î¼òµ¥µÄÐÒéÉú³ÉµÄ·½·¨ÊDzÉÓÃÐÒé±àÒ빤¾ß£¬³£ÓõÄÓÐRpcgen£¬ÎÒ
»áÔÚºóÃæÊµÀýÖÐÏêϸÃèÊöÆäʹÓ÷½·¨¡£
b¡¢¿ª·¢¿Í»§¶Ë³ÌÐò¡£
c¡¢¿ª·¢·þÎñÆ÷¶Ë³ÌÐò¡£
¿ª·¢¿Í»§¶ËºÍ·þÎñÆ÷¶ËµÄ³ÌÐòʱ£¬RPCÌṩÁËÎÒÃDz»Í¬²ã´ÎµÄ¿ª·¢Àý³Ìµ÷Óýӿڡ£²»Í¬²ã´ÎµÄ½Ó¿ÚÌṩÁ˶ÔRPC²»Í¬³Ì¶È¿ØÖÆ¡£Ò»°ã¿É·ÖΪ5¸öµÈ¼¶µÄ±à³Ì½Ó¿Ú£¬½ÓÏÂÀ´ÎÒÃÇ·Ö±ðÌÖÂÛһϸ÷²ãËùÌṩµÄ¹¦Äܺ¯Êý¡£
1¡¢¼òµ¥²ãÀý³Ì
¼òµ¥²ãÊÇÃæÏòÆÕͨRPCÓ¦Óã¬ÎªÁË¿ìËÙ¿ª·¢RPCÓ¦Ó÷þÎñ¶øÉè¼ÆµÄ£¬ËûÌṩÁËÈçϹ¦Äܺ¯Êý¡£

2¡¢¸ß²ãÀý³Ì ÔÚÕâÒ»²ã£¬³ÌÐòÐèÒªÔÚ·¢³öµ÷ÓÃÇëÇóǰÏÈ´´½¨Ò»¸ö¿Í»§¶Ë¾ä±ú£¬»òÊÇÔÚÕìÌýÇëÇóǰÏȽ¨Á¢Ò»¸ö·þÎñÆ÷¶Ë¾ä±ú¡£³ÌÐòÔڸòã¿ÉÒÔ×ÔÓɵĽ«×Ô¼ºµÄÓ¦ÓðóÔÚËùÓеĴ«Êä¶Ë¿ÚÉÏ£¬ËüÌṩÁËÈçϹ¦Äܺ¯Êý¡£

3¡¢Öмä²ãÀý³Ì Öмä²ãÏò³ÌÐòÌṩ¸üΪÏêϸµÄRPC¿ØÖƽӿÚ,¶øÕâÒ»²ãµÄ´úÂë±äµÃ¸üΪ¸´ÔÓ£¬µ«ÔËÐÐÒ²¸üΪÓÐЧ£¬ËüÌṩÁËÈçϹ¦Äܺ¯Êý¡£

4¡¢×¨¼Ò²ãÀý³Ì Õâ²ãÌṩÁ˸ü¶àµÄһϵÁÐÓë´«ÊäÏà¹ØµÄ¹¦Äܵ÷Óã¬ËüÌṩÁËÈçϹ¦Äܺ¯Êý¡£

5¡¢µ×²ãÀý³Ì ¸Ã²ãÌṩÁËËùÓжԴ«ÊäÑ¡Ïî½øÐпØÖƵĵ÷Óýӿڣ¬ËüÌṩÁËÈçϹ¦Äܺ¯Êý¡£

............ÒýÓÃÎÄ×Öδ¾¡£¬ÏêϸÇë²Î¼ûhttp://www.yuanma.org/data/2006/0918/article_1560.htm ¹ØÓÚ¡°RPCÓïÑÔ¡± RPCÓïÑÔÒ²ÊÇÒ»ÖÖרÃŵıà³ÌÓïÑÔ£¬µ±È»ÕâÀïÎÒÃDz»ÐèÒªÖªµÀÌ«¶à£¬Ö»ÐèÒªÄÜ¿´¶®ÏÂÃæÕâÖÖ»ù±¾½á¹¹¾ÍÐÐÁË£º
program TESTPROG { version VERSION { string TEST(string) = 1; } = 1; } = 87654321; |
ÕâÀïTESTPROGºÍVERSIONÊÇÁ½¸ö±äÁ¿£¬ÓÃÓÚ±êʶһ¸öµ¥¶ÀµÄRPC½Ó¿Ú¡£Õâ±»RPC·þÎñ³ÌÐò£¬±ÈÈçportmapÓõ½£¬ÎÒÃÇ¿ÉÒÔ²»ÓùØÐÄ£¬±äÁ¿Ãû×ÖÒ²ÊÇËæ±ãÈ¡µÄ¡£µ«È¡ÖµÒªÔÚÄãµÄϵͳÖÐÊÇΨһµÄ¡£
¡°string TEST(string) =
1;¡±ÕâÒ»ÐÐ˵Ã÷ÓÐÁ½¸öº¯Êýtest_VERSIONºÍtest_VERSION_svc£¬ÕâÀïÓÉÓÚVERSION±äÁ¿Îª1£¬ËùÒÔº¯ÊýÃûΪtest_1ºÍtest_1_svc£¬ÕâÁ½¸öº¯ÊýÓÃÓÚÔÚ·þÎñÆ÷¶ËºÍ¿Í»§¶ËʵÏÖµ÷Ó㬼´£ºÔÚ¿Í»§¶Ëµ÷ÓÃtest_1º¯Êý£¬·þÎñÆ÷¶Ëµ÷ÓÃtest_1_svcº¯Êý´¦Àí²¢·µ»Ø¡£
º¯ÊýµÄÀàÐÍÊÇstring£¬RPCÓïÑÔÖÐstring¼´CÀïÃæµÄÒ»¸ö×Ö·û´®¡£ËùÒÔÉÏÊöº¯ÊýÓÐÒ»¸ö×Ö·û´®×÷Ϊ²ÎÊý´«µÝ£¬Í¬Ê±Òª·µ»Ø×Ö·û´®¡£¼´£ºchar
** test_1(char **argp, CLIENT *clnt) ºÍ char **test_1_svc(char
**argp, struct svc_req *rqstp)
ͬÀí£¬Èç¹ûÉùÃ÷ÊÇÕâÑùµÄ£º
program RDICTPROG /* name of remote program ( not used ) */ { version RDICTVERS /* declaration of version ( see below ) */ { int INITW ( void ) = 1; /* first procedure in this program */ int INSERTW ( string ) = 2; /* second procedure in this program */ int DELETEW ( string ) = 3; /* third procedure in this program */ int LOOKUPW ( string ) = 4; /* fourth procedure in this program */ } = 1; /* definition of the program version */ } = 0x30090949; /* remote program number ( must be unique ) */ |
Ôò˵Ã÷Õâ¸öRPCÖÐÓÐËĸöº¯Êý¿ÉÓ㬼´¿Í»§¶Ë¿ÉÒÔµ÷ÓÃinitw_1¡¢insertw_1¡¢deletew_1¡¢lookupw_1Ëĸöº¯ÊýÀ´Ïò·þÎñ¶Ë·¢ËÍÏûÏ¢£¬·þÎñ¶Ë¿ÉÒÔÓÃinitw_1_svc¡¢insertw_1_svc¡¢deletew_1_svc¡¢lookupw_1_svcËĸöº¯ÊýÀ´´¦ÀíÇëÇó²¢·µ»Ø½á¹û¡£
ÔÈÎÎñ
¼ÙÉèÏÖÔÚÓÐÕâÑùÒ»¸ö³ÌÐò£¬Ô´´úÂëÈçÏ£º
/* dict.c -- main, initw, nextin, insertw, deletew, lookupw */ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #define MAXWORD 50 /* maximum length of a command or word */ #define DICTSIZ 100 /* maximum number of entries in dictionary. */ char dict[DICTSIZ][MAXWORD + 1]; /* storage for a dictionary of words */ int nwords = 0; /* number of words in the dictionary */ /* º¯ÊýÔÐÍ */ int nextin(char *cmd, char *word); int initw(void); int insertw(const char *word); int deletew(const char *word); int lookupw(const char *word); /* ------------------------------------------------------------------ * main -- insert, delete, or lookup words in a dictionary as specified * ------------------------------------------------------------------ */ int main(int argc, char *argv[]) { char word[MAXWORD + 1]; /* space to hold word from input line */ char cmd; int wordlen; /* length of input word */ printf("Please input:\n"); while (1) { wordlen = nextin(&cmd, word); if (wordlen < 0) { exit(0); } switch (cmd) { case 'I': /* ³õʼ»¯ */ initw(); printf("Dictionary initialized to empty.\n"); break; case 'i': /* ²åÈë */ insertw(word); printf("%s inserted.\n", word); break; case 'd': /* ɾ³ý */ if (deletew(word)) { printf("%s deleted.\n", word); } else { printf("%s not found.\n", word); } break; case 'l': /* ²éѯ */ if (lookupw(word)) { printf("%s was found.\n", word); } else { printf("%s was not found.\n", word); } break; case 'q': /* Í˳ö */ printf("Program quits.\n"); exit(0); break; default: /* ·Ç·¨ÊäÈë */ printf("command %c invalid.\n", cmd); break; } /* end of switch */ } /* end of while */ return 0; } /* end of main */ /* ------------------------------------------------------------------
* nextin -- read a command and(possibly) a word
from the next input line
* ------------------------------------------------------------------
*/
int nextin(char *cmd, char *word)
{
int i, ch;
ch = getc(stdin);
while (isspace(ch)) {
ch = getc(stdin);
} /* end of while */
if (ch == EOF) {
return (-1);
}
*cmd = (char) ch;
ch = getc(stdin);
while (isspace(ch)) {
ch = getc(stdin);
} /* end of while */
if (ch == EOF) {
return (-1);
}
if (ch == '\n') {
return (0);
}
i = 0;
while (!isspace(ch)) {
if (++i > MAXWORD) {
printf("error: word too long.\n");
exit(1);
}
*word++ = ch;
ch = getc(stdin);
} /* end of while */
*word = '\0'; /* ÔÀ´µÄ´úÂëÕâÀïÓÐÎÊÌâ */
return i;
} /* end of nextin */ /* ------------------------------------------------------------------
* initw -- initialize the dictionary to contain
no words at all
* ------------------------------------------------------------------
*/
int initw(void)
{
nwords = 0;
return 1;
} /* end of initw */ /* ------------------------------------------------------------------
* insertw -- insert a word in the dictionary
* ------------------------------------------------------------------
*/
int insertw(const char *word)
{
strcpy(dict[nwords], word);
nwords++;
return (nwords);
} /* end of insertw */ /* ------------------------------------------------------------------
* deletew -- delete a word from the dictionary
* ------------------------------------------------------------------
*/
int deletew(const char *word)
{
int i;
for (i = 0; i < nwords; i++) {
if (strcmp(word, dict[i]) == 0) {
nwords--;
strcpy(dict[i], dict[nwords]);
return (1);
}
} /* end of for */
return (0);
} /* end of deletew */ /* ------------------------------------------------------------------
* lookupw -- look up a word in the dictionary
* ------------------------------------------------------------------
*/
int lookupw(const char *word)
{
int i;
for (i = 0; i < nwords; i++) {
if (strcmp(word, dict[i]) == 0) {
return (1);
}
} /* end of for */
return (0);
} /* end of lookupw */ |
ÕâÊÇÒ»¸ö¼òµ¥µÄ×Öµä³ÌÐò£¬¼´³ÌÐòÔËÐÐÆðÀ´ÒÔºóά»¤×ÅÒ»¸ö×Öµä¿â£¬Óû§¿ÉÒÔÏòÀïÃæÌí¼Ó´ÊÓҲ¿ÉÒÔ²éѯ»òɾ³ý´ÊÓï¡£
µ±È»£¬Õâ¸ö³ÌÐòÖ»ÄÜÔÚͬһ̨Ö÷»úÉÏÔËÐС£³ÌÐòÕû¸öÔËÐйý³ÌÖУ¬Ö»ÐèÒªÍê³ÉÈçϼ¸¸ö²½Ö裺
A¡¢½ÓÊÜÓû§ÊäÈ룻
B¡¢·ÖÎöÓû§ÊäÈë¾ö¶¨ÊÇ·ñ½øÐÐÏÂÃæµÄ²½Ö裺
1¡¢³õʼ»¯Êý¾Ý¿â£»
2¡¢ÏòÊý¾Ý¿âÌí¼Ó´ÊÓ
3¡¢²éѯ»òɾ³ý´ÊÓï ÈÎÎñ·Ö½â ´ó¼Ò¿ÉÒÔÏëµ½£¬¶ÔÓÚÒ»¸ö´óÐÍϵͳ£¬±ÈÈçÐèÒªÓкܶàÈËά»¤Õâ¸öϵͳµÄÊý¾Ý¡£ÏóÉÏÃæÕâÑù¶ÀÁ¢µÄ³ÌÐò¾Í²»ÊÊÓÃÁË£¬ÐèÒª×ö³É·Ö²¼Ê½ÏµÍ³£º ¼´Ò»¸ö·þÎñÆ÷ά»¤×ÅÊý¾Ý¿â£¬Èκοͻ§¶Ë¶¼¿ÉÒÔ½ÓÊÜÓû§ÇëÇ󣬿ͻ§¶Ë·ÖÎöÓû§ÃüÁîºóÌá½»¸ø·þÎñÆ÷È¥´¦Àí¡£ ËùÒÔÎÒÃÇ¿ÉÄÜ»á°Ñ³ÌÐò·Ö³ÉÁ½²¿·Ö£º ¿Í»§¶Ë£º½ÓÊÜÓû§ÊäÈ룬²¢ÅжÏÓû§ÊäÈëÄÚÈݵÄÕýÈ·ÐÔ£¬Ïò·þÎñÆ÷Ìá½»Êý¾Ý£¬µÈ·þÎñÆ÷·µ»ØÏûÏ¢ ·þÎñÆ÷¶Ë£ºÎ¬»¤Êý¾Ý£¬½ÓÊܿͻ§¶ËÃüÁî²¢Ö´Ðк󷵻ؽá¹û¡£ ËùÒÔÎÒÃǰÑÉÏÃæÕâ¸ö³ÌÐò·Ö½â³ÉÏÂÃæÁ½²¿·Ö£º
/* dict1.c -- main, nextin */ #include <stdio.h> #include <stdlib.h> #define MAXWORD 50 /* maximum length of a command or word */ /* ------------------------------------------------------------------ * main -- insert, delete, or lookup words in a dictionary as specified * ------------------------------------------------------------------ */ int main(int argc, char *argv[]) { char word[MAXWORD + 1]; /* space to hold word from input line */ char cmd; int wordlen; /* length of input word */ printf("Please input:\n"); while (1) { wordlen = nextin(&cmd, word); if (wordlen < 0) { exit(0); } switch (cmd) { case 'I': /* ³õʼ»¯ */ initw(); printf("Dictionary initialized to empty.\n"); break; case 'i': /* ²åÈë */ insertw(word); printf("%s inserted.\n", word); break; case 'd': /* ɾ³ý */ if (deletew(word)) { printf("%s deleted.\n", word); } else { printf("%s not found.\n", word); } break; case 'l': /* ²éѯ */ if (lookupw(word)) { printf("%s was found.\n", word); } else { printf("%s was not found.\n", word); } break; case 'q': /* Í˳ö */ printf("Program quits.\n"); exit(0); break; default: /* ·Ç·¨ÊäÈë */ printf("command %c invalid.\n", cmd); break; } /* end of switch */ } /* end of while */ return 0; } /* end of main */ /* ------------------------------------------------------------------
* nextin -- read a command and(possibly) a word
from the next input line
* ------------------------------------------------------------------
*/
int nextin(char *cmd, char *word)
{
int i, ch;
ch = getc(stdin);
while (isspace(ch)) {
ch = getc(stdin);
} /* end of while */
if (ch == EOF) {
return (-1);
}
*cmd = (char) ch;
ch = getc(stdin);
while (isspace(ch)) {
ch = getc(stdin);
} /* end of while */
if (ch == EOF) {
return (-1);
}
if (ch == '\n') {
return (0);
}
i = 0;
while (!isspace(ch)) {
if (++i > MAXWORD) {
printf("error: word too long.\n");
exit(1);
}
*word++ = ch;
ch = getc(stdin);
} /* end of while */
*word = '\0';
return i;
} /* end of nextin */ |
ºÍ
/* dict2.c -- initw, insertw, deletew, lookupw */ #include <string.h> #define MAXWORD 50 /* maximum length of a command or word */ #define DICTSIZ 100 /* maximum number of entries in dictionary. */ char dict[DICTSIZ][MAXWORD + 1]; /* storage for a dictionary of words */ int nwords = 0; /* number of words in the dictionary */ /* ------------------------------------------------------------------ * initw -- initialize the dictionary to contain no words at all * ------------------------------------------------------------------ */ int initw(void) { nwords = 0; return 1; } /* end of initw */ /* ------------------------------------------------------------------
* insertw -- insert a word in the dictionary
* ------------------------------------------------------------------
*/
int insertw(const char *word)
{
strcpy(dict[nwords], word);
nwords++;
return (nwords);
} /* end of insertw */ /* ------------------------------------------------------------------
* deletew -- delete a word from the dictionary
* ------------------------------------------------------------------
*/
int deletew(const char *word)
{
int i;
for (i = 0; i < nwords; i++) {
if (strcmp(word, dict[i]) == 0) {
nwords--;
strcpy(dict[i], dict[nwords]);
return (1);
}
} /* end of for */
return (0);
} /* end of deletew */ /* ------------------------------------------------------------------
* lookupw -- look up a word in the dictionary
* ------------------------------------------------------------------
*/
int lookupw(const char *word)
{
int i;
for (i = 0; i < nwords; i++) {
if (strcmp(word, dict[i]) == 0) {
return (1);
}
} /* end of for */
return (0);
} /* end of lookupw */ |
ÕâÁ½²¿·Ö´úÂëÖ»ÊÇÔÚ¹¦ÄÜÉÏʵÏÖÁË·ÖÀ룬ÏÔȻʵÏÖͨѶµÄ²¿·Ö»¹Ã»ÓУ¬ÏÂÃæÎÒÃÇÀûÓÃRPCÀ´¿ìËÙʵÏÖͨѶ¡£ ÀûÓÃRPCʵÏÖ·Ö²¼Ê½ÏµÍ³
Ê×ÏÈ£¬½¨Á¢Ò»¸öRPCÔ´Îļþ£¬Ô´´úÂërdict.xÈçÏ£º
/* rdict.x */ /* RPC declarations for dictionary program */ const MAXWORD = 10; /* maximum length of a command or word */ const DICTSIZ = 3; /* number of entries in dictionary */ struct example /* unused structure declared here to */ { int exfield1; /* illustrate how rpcgen builds XDR */ char exfield2; /* routines to convert structures */ }; /* ------------------------------------------------------------------ * RDICTPROG -- remote program that provides insert, delete, and lookup * ------------------------------------------------------------------ */ program RDICTPROG /* name of remote program ( not used ) */ { version RDICTVERS /* declaration of version ( see below ) */ { int INITW ( void ) = 1; /* first procedure in this program */ int INSERTW ( string ) = 2; /* second procedure in this program */ int DELETEW ( string ) = 3; /* third procedure in this program */ int LOOKUPW ( string ) = 4; /* fourth procedure in this program */ } = 1; /* definition of the program version */ } = 0x30090949; /* remote program number ( must be unique ) */ |
È»ºóÓÃÏÂÁÐÃüÁî²úÉú·þÎñÆ÷¶Ëº¯Êýrdict_srv_func.c£º
rpcgen -Ss -o rdict_srv_func.c rdict.x
È»ºóÓÃÏÂÁÐÃüÁî²úÉú¿Í»§¶Ë³ÌÐòrdict_client.c£º
rpcgen -Sc -o rdict_client.c rdict.x
È»ºóÓÃÏÂÁÐÃüÁî²úÉúMakefile£º
rpcgen -Sm rdict.x > MakefileMakefileÎļþÔÄÚÈÝÈçÏ£º
# This is a template Makefile generated by rpcgen
# Parameters
CLIENT = rdict_client
SERVER = rdict_server
SOURCES_CLNT.c =
SOURCES_CLNT.h =
SOURCES_SVC.c =
SOURCES_SVC.h =
SOURCES.x = rdict.x
TARGETS_SVC.c = rdict_svc.c rdict_xdr.c
TARGETS_CLNT.c = rdict_clnt.c rdict_xdr.c
TARGETS = rdict.h rdict_xdr.c rdict_clnt.c rdict_svc.c
OBJECTS_CLNT = $(SOURCES_CLNT.c:%.c=%.o) $(TARGETS_CLNT.c:%.c=%.o)
OBJECTS_SVC = $(SOURCES_SVC.c:%.c=%.o) $(TARGETS_SVC.c:%.c=%.o)
# Compiler flags
CFLAGS += -g
LDLIBS += -lnsl
RPCGENFLAGS =
# Targets
all : $(CLIENT) $(SERVER)
$(TARGETS) : $(SOURCES.x)
rpcgen $(RPCGENFLAGS) $(SOURCES.x)
$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h)
$(TARGETS_CLNT.c)
$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h)
$(TARGETS_SVC.c)
$(CLIENT) : $(OBJECTS_CLNT)
$(LINK.c) -o $(CLIENT) $(OBJECTS_CLNT) $(LDLIBS)
$(SERVER) : $(OBJECTS_SVC)
$(LINK.c) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)
clean:
$(RM) core $(TARGETS) $(OBJECTS_CLNT) $(OBJECTS_SVC)
$(CLIENT) $(SERVER) |
¶¯ÊÖÐÞ¸ÄMakefile£¬Ð޸ĺóÄÚÈÝÈçÏ£º
# This is a template Makefile generated by rpcgen
# Parameters
CLIENT = rdict_client
SERVER = rdict_server
SOURCES_CLNT.c =
SOURCES_CLNT.h =
SOURCES_SVC.c =
SOURCES_SVC.h =
SOURCES.x = rdict.x
TARGETS_SVC.c = rdict_svc.c rdict_xdr.c rdict_srv_func.c
TARGETS_CLNT.c = rdict_clnt.c rdict_xdr.c rdict_client.c
TARGETS = rdict.h rdict_xdr.c rdict_clnt.c rdict_svc.c
OBJECTS_CLNT = $(SOURCES_CLNT.c:%.c=%.o) $(TARGETS_CLNT.c:%.c=%.o)
OBJECTS_SVC = $(SOURCES_SVC.c:%.c=%.o) $(TARGETS_SVC.c:%.c=%.o)
# Compiler flags
CFLAGS += -g
LDLIBS += -lnsl
RPCGENFLAGS =
# Targets
all : $(CLIENT) $(SERVER)
$(TARGETS) : $(SOURCES.x)
rpcgen $(RPCGENFLAGS) $(SOURCES.x)
$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h)
$(TARGETS_CLNT.c)
$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h)
$(TARGETS_SVC.c)
$(CLIENT) : $(OBJECTS_CLNT)
$(LINK.c) -o $(CLIENT) $(OBJECTS_CLNT) $(LDLIBS)
$(SERVER) : $(OBJECTS_SVC)
$(LINK.c) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)
clean:
$(RM) core $(TARGETS) $(OBJECTS_CLNT) $(OBJECTS_SVC)
$(CLIENT) $(SERVER) *~ |
Ð޸Ŀͻ§¶ËÔ´´úÂërdict_client.c£¬°Ñ½ÓÊÜÓû§ÊäÈë²¢·ÖÎöÓû§ÊäÈëÄÚÈݵIJ¿·Ö¼Óµ½³ÌÐòÖÐÀ´¡£Ð޸ĺóµÄ´úÂëΪ£º
/* * 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 "rdict.h"
/* ------------------------------------------------------------------
* nextin -- read a command and(possibly) a word
from the next input line
* ------------------------------------------------------------------
*/
int nextin(char *cmd, char *word)
{
int i, ch;
ch = getc(stdin);
while (isspace(ch)) {
ch = getc(stdin);
} /* end of while */
if (ch == EOF) {
return (-1);
}
*cmd = (char) ch;
ch = getc(stdin);
while (isspace(ch)) {
ch = getc(stdin);
} /* end of while */
if (ch == EOF) {
return (-1);
}
if (ch == '\n') {
return (0);
}
i = 0;
while (!isspace(ch)) {
if (++i > MAXWORD) {
printf("error: word too long.\n");
exit(1);
}
*word++ = ch;
ch = getc(stdin);
} /* end of while */
*word = '\0';
return i;
} /* end of nextin */
void rdictprog_1(char *host)
{
CLIENT *clnt;
int *result_1;
char *initw_1_arg;
int *result_2;
char *insertw_1_arg;
int *result_3;
char *deletew_1_arg;
int *result_4;
char *lookupw_1_arg;
#ifndef DEBUG
clnt = clnt_create(host, RDICTPROG, RDICTVERS,
"udp");
if (clnt == NULL) {
clnt_pcreateerror(host);
exit(1);
}
#endif /* DEBUG */
char word[MAXWORD + 1]; /* space to hold word
from input line */
char cmd;
int wordlen; /* length of input word */
while (1) {
printf("\nPlease input:");
wordlen = nextin(&cmd, word);
if (wordlen < 0) {
exit(0);
}
/* printf("\nYour cmd is:%c, your word is:%s\n",
cmd, word); */
switch (cmd) {
case 'I': /* ³õʼ»¯ */
result_1 = initw_1((void *) &initw_1_arg,
clnt);
/* printf("\nYour result is:%d\n", *result_1);
*/
if (result_1 == (int *) NULL)
clnt_perror(clnt, "call failed");
else
if(*result_1 ==0) printf("Dictionary initialized
to empty.\n");
else printf("Dictionary have already initialized.\n");
break;
case 'i': /* ²åÈë */
insertw_1_arg = word;
result_2 = insertw_1(&insertw_1_arg, clnt);
/* printf("\nYour result is:%d, your string
is:%s(%d)\n", *result_2, insertw_1_arg, strlen(insertw_1_arg));
*/
if (result_2 == (int *) NULL)
clnt_perror(clnt, "call failed");
else
printf("%s inserted.\n", word);
break;
case 'd': /* ɾ³ý */
deletew_1_arg = word;
result_3 = deletew_1(&deletew_1_arg, clnt);
/* printf("\nYour result is:%d, your string
is:%s(%d)\n", *result_3, deletew_1_arg, strlen(deletew_1_arg));
*/
if (result_3 == (int *) NULL)
clnt_perror(clnt, "call failed");
else
printf("%s deleted.\n", word);
break;
case 'l': /* ²éѯ */
lookupw_1_arg = word;
result_4 = lookupw_1(&lookupw_1_arg, clnt);
/* printf("\nYour result is:%d, your string
is:%s(%d)\n", *result_4, lookupw_1_arg, strlen(lookupw_1_arg));
*/
if (result_4 == (int *) NULL)
clnt_perror(clnt, "call failed");
else
if(*result_4 ==0) printf("%s found.\n",
word);
else printf("%s not found.\n", word);
break;
case 'q': /* Í˳ö */
printf("Program quits.\n");
exit(0);
break;
default: /* ·Ç·¨ÊäÈë */
printf("Command %c(%s) invalid.\n",
cmd, word);
break;
} /* end of switch */
} /* end of while */
#ifndef DEBUG
clnt_destroy(clnt);
#endif /* DEBUG */
}
int main(int argc, char *argv[])
{
char *host;
if (argc < 2) {
printf("usage: %s server_host\n", argv[0]);
exit(1);
}
host = argv[1];
rdictprog_1(host);
exit(0);
} |
ͬʱÐ޸ķþÎñÆ÷¶Ë´úÂërdict_srv_func.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 "rdict.h"
char dict[DICTSIZ][MAXWORD + 1]; /* storage for
a dictionary of words */
int nwords = 0; /* number of words in the dictionary
*/
char init_bool = 0;
int initw(void)
{
if(init_bool) return 1;
nwords = 0;
init_bool = 1;
return 0;
} /* end of initw */
/* ------------------------------------------------------------------
* insertw -- insert a word in the dictionary
* ------------------------------------------------------------------
*/
int insertw(const char *word)
{
strcpy(dict[nwords%DICTSIZ], word);
nwords++;
return (nwords);
} /* end of insertw */
/* ------------------------------------------------------------------
* deletew -- delete a word from the dictionary
* ------------------------------------------------------------------
*/
int deletew(const char *word)
{
int i;
for (i = 0; i < nwords; i++) {
if (strcmp(word, dict[i]) == 0) {
nwords--;
strcpy(dict[i], dict[nwords]);
return (1);
}
} /* end of for */
return (0);
} /* end of deletew */
/* ------------------------------------------------------------------
* lookupw -- look up a word in the dictionary
* ------------------------------------------------------------------
*/
int lookupw(const char *word)
{
int i;
for (i = 0; i < nwords; i++) {
if (strcmp(word, dict[i]) == 0) {
return 0;
}
} /* end of for */
return 1;
} /* end of lookupw */
int *initw_1_svc(void *argp, struct svc_req *rqstp)
{
static int result;
/*
* insert server code here
*/
result = initw();
return &result;
}
int *insertw_1_svc(char **argp, struct svc_req
*rqstp)
{
static int result;
/*
* insert server code here
*/
result = insertw(*argp);
return &result;
}
int *deletew_1_svc(char **argp, struct svc_req
*rqstp)
{
static int result;
/*
* insert server code here
*/
result = deletew(*argp);
return &result;
}
int *lookupw_1_svc(char **argp, struct svc_req
*rqstp)
{
static int result;
/*
* insert server code here
*/
result = lookupw(*argp);
return &result;
} |
ÖÁ´Ë£¬³ÌÐò×öºÃÁË¡£ÊäÈëÒ»¸ömakeÃüÁî¾Í¿ÉÒÔÉú³Étest_serverºÍtest_clientÕâÁ½¸ö¿ÉÖ´ÐгÌÐòÁË¡£
ÔÚһ̨»úÆ÷ÉÏÔËÐÐ./test_server³ÌÐò£¬ÔÚÁíÍâµÄ¿Í»§»úÉÏÔËÐÐ./test_client
server_ip¾Í¿ÉÒÔÁË¡£ÕâÀïserver_ipÊÇÔËÐÐ×Åtest_server³ÌÐòµÄÖ÷»úµÄIPµØÖ·(±¾»ú²âÊÔΪ127.0.0.1)¡£ ÁíÍâ²¹³ä£¬ÈôÆô¶¯server³öÏÖcan not register service:RPC´íÎó£¬ÒòΪubuntuϵͳδ°²×°portmapËùÖ£¬Ö´ÐÐÃüÁsudo
apt-get install portmapÖ®ºó¾Í¿ÉÒÔ¡£ ֻΪ·ÖÏí¡£
|