分类:
2009-12-22 18:41:35
program TESTPROG { version VERSION { string TEST(string) = 1; } = 1; } = 87654321; |
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 ) */ |
/* dict.c -- main, initw, nextin, insertw, deletew, lookupw */ #include #include #include #include #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 */ |
/* dict1.c -- main, nextin */ #include #include #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 #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 */ |
/* 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 ) */ |
rpcgen -Ss -o rdict_srv_func.c rdict.x |
rpcgen -Sc -o rdict_client.c rdict.x |
rpcgen -Sm rdict.x > 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 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) |
# 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) *~ |
/* * 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); } |
/* * 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; } |