2015-03-03 75 views
0

我想從RPC客戶端發送字符指針到服務器的rpcgen使下述的是服務器和客戶端程序傳遞字符指針從客戶機到服務器的rpcgen

RPC程序RPC根

struct clientinput { 
    char * optype; 
    char * word; 
}; 

program DICTIONARY_PROG { 
    version DICTIONARY_VERS { 
     int USEDICTIONARY(clientinput) = 1; 
    } = 1; 
} = 0x23451113; 

RPC服務器

#include "dictionary.h" 

    int * 
    usedictionary_1_svc(clientinput *argp, struct svc_req *rqstp) 
    { 
     static int result; 
     char *optype = (char*)malloc (10); 
     char *word = (char*)malloc (10); 

     char *a = argp->optype; 
     char *b = argp->word; 

     printf("Optype is %s\n", a); 
     printf("Word is %s\n", b); 

     printf("The optype is %s\n", strcpy(optype, argp->optype)); 
     printf("The word is %s\n", strcpy(word, argp->word)); 
     /* 
     * insert server code here 
     */ 

     return &result; 
    } 

RPC客戶端

#include "dictionary.h" 


void 
dictionary_prog_1(char *host, char *optype, char *word) 
{ 
    CLIENT *clnt; 
    int *result_1; 
    clientinput usedictionary_1_arg; 

    //strcpy(usedictionary_1_arg.optype, optype); 
    //strcpy(usedictionary_1_arg.word, word); 

    usedictionary_1_arg.optype = optype; 
    usedictionary_1_arg.word = word; 

    printf("Optype input is %s\n",usedictionary_1_arg.optype); 
    printf("Word input is %s \n",usedictionary_1_arg.word); 

#ifndef DEBUG 
    clnt = clnt_create (host, DICTIONARY_PROG, DICTIONARY_VERS, "udp"); 
    if (clnt == NULL) { 
     clnt_pcreateerror (host); 
     exit (1); 
    } 
#endif /* DEBUG */ 

    result_1 = usedictionary_1(&usedictionary_1_arg, clnt); 
    if (result_1 == (int *) NULL) { 
     clnt_perror (clnt, "call failed"); 
    } 
#ifndef DEBUG 
    clnt_destroy (clnt); 
#endif /* DEBUG */ 
} 


int 
main (int argc, char *argv[]) 
{ 
    char *host, *optype, *word; 

    if (argc < 2) { 
     printf ("usage: %s server_host\n", argv[0]); 
     exit (1); 
    } 
    host = argv[1]; 
    optype = argv[2]; 
    word = argv[3]; 

    dictionary_prog_1 (host,optype,word); 
exit (0); 
} 

這裏是在服務器端輸出

Optyep is a 
Word is e 
The optype is a 
The word is e 

我在這裏遇到的問題是,我從服務器傳送到客戶端的字符指針只有第一個字符被打印。我嘗試過使用字符指針的可能組合,但找不到原因。那麼有人可以幫我找出原因嗎?

回答

1

從查看文檔,RPCGen需要一些幫助來區分單個字符參數和實際的字符數組,因爲所有參數都作爲指針傳遞。要讓它知道你想要的字符數組,又名串,你需要使用string關鍵字在你的結構聲明如下所示:

struct clientinput { 
    string optype<>; 
    string word<>; 
}; 

answer解釋它,以及這blog post也有類似的例子,你想完成什麼。

+0

同意。那麼我如何使用我的C程序來檢索值?我的意思是應該將它處理並轉換爲char *。我對c相對比較陌生,這就是爲什麼我可能聽起來不合適 – 2015-03-03 11:09:10

+0

你不需要施放。 string關鍵字將使用'char *'作爲成員類型生成代碼,但rpc調用的序列化/反序列化代碼會將它們視爲以空字符結束的字符串,而不是單個字符。我非常肯定客戶端的'print'打印整個字符串,但編組發送一個字符。 – 2015-03-03 11:49:01

+0

在我的答案中添加了一個鏈接,向您展示一個類似於您的用例的工作示例。 – 2015-03-03 11:51:03

相關問題