2015-11-13 71 views
0

我怎麼能正確地發送一個結構從服務器到客戶端在ansi-c sun-rpc?發送一個結構數組與太陽rpc從服務器到客戶端

在我 test.x IDL文件

我定義的字符串的結構體簇和一個int 和類型的簇是簇元素的可變長度數組:

struct cluster { 
    string name<255>; 
    int debuglevel; 
}; 

typedef cluster clusters<32>; 

我再變通過像的rpcgen

test_server.c

clusters * 
test_1_svc(void *argp, struct svc_req *rqstp) 
{ 

    static clusters result; 

    cluster cl1, cl2; 

    cl1.name="cl1"; 
    cl1.debuglevel="1"; 
    cl2.name="cl2"; 
    cl2.debuglevel="2"; 

    cluster clist[2]; 

    clist[0]=cl1; 
    clist[1]=cl2; 

    result.clusters_len = 2; 
    result.clusters_val = &clist; 

    /* 
    * insert server code here 
    */ 

    return(&result); 
} 
產生的存根

test_client.c

test_prog_1(char* host) 
{ 
    CLIENT *clnt; 
    clusters *result_1; 
    char* test_1_arg; 
    clnt = clnt_create(host, test_PROG, test_VERS, "udp"); 
    if (clnt == NULL) { 
     clnt_pcreateerror(host); 
     exit(1); 
    } 
    result_1 = test_1((void*)&test_1_arg, clnt); 
    if (result_1 == NULL) { 
     clusters* rec_cls = malloc(2*sizeof(struct cluster)); 
     if(xdr_clusters(&result_1, rec_cls)){ 
       printf("got xdr_clusters"); 
     } 
     clnt_perror(clnt, "call failed:"); 
    } 
    clnt_destroy(clnt); 
} 

兩個編譯,但一個或兩個請求,由客戶端和對xdr_clusters函數不返回true客戶方運行後,服務器經常出現segfaults。這似乎是某種內存管理不當,我也不確定是否正確處理服務器端的序列化。

我剛剛填寫result.clusters_len和result.clusters_val與此時,相應的值,如他們在test.h(通過的rpcgen)定義:

typedef struct { 
    u_int clusters_len; 
    cluster *clusters_val; 
} clusters; 

我必須充分利用在xdr_clusters的服務器端爲此來正確地序列化結果?

謝謝

回答

0

好吧,我想通我的錯誤,讓他們總結:

  • 知道如何正確初始化一個int(當然沒有引號...)
  • 忘記那clist廢話,只是malloc結果結構的內部指針直接
  • 閱讀該死的編譯器警告:當它告訴你,有隱式聲明的函數,你不想隱式聲明,那麼可能有東西缺少,在我的情況下,我需要包含stdlib.h和stdio.h以獲取服務器和客戶端存根的malloc,printf和退出函數。
  • 在客戶端:爲什麼我們應該做任何事情,除非在結果爲NULL時拋出錯誤?看到下面的新的客戶端代碼來檢查正確的結果打印

test_server.c

test_1_svc(void *argp, struct svc_req *rqstp){ 

     static clusters result; 

     cluster cl1, cl2; 

     cl1.name="cl1"; 
     cl1.debuglevel=1; 
     cl2.name="cl2"; 
     cl2.debuglevel=2; 

     result.clusters_len = 2; 
     result.clusters_val = malloc(2*sizeof(struct cluster)); 

     result.clusters_val[0]=cl1; 
     result.clusters_val[1]=cl2; 

     return(&result); 
    } 

test_client.c

test_prog_1(char* host) 
{ 
    CLIENT *clnt; 
    clusters *result_1; 
    char* test_1_arg; 
    clnt = clnt_create(host, test_PROG, test_VERS, "udp"); 
    if (clnt == NULL) { 
     clnt_pcreateerror(host); 
     exit(1); 
    } 
    result_1 = test_1((void*)&test_1_arg, clnt); 
    if (result_1 == NULL) { 
     clnt_perror(clnt, "call failed:"); 
    }else{ 
     printf("I got %d cluster structs in an array\n",result_1->clusters_len); 
     int j; 
     for(j=0;j<result_1->clusters_len;j++){ 
      printf("cluster #%d: %[email protected] %d\n",j,result_1->clusters_val[j].name,result_1->clusters_val[j].debuglevel); 
     } 
    } 
    clnt_destroy(clnt); 
} 

結果,我們得到了一些不錯的值在客戶端打印 ,當然在服務器端不再有段錯誤:

lars$ ./test_client localhost 
I got 2 cluster structs in an array 
cluster #0: [email protected] 1 
cluster #1: [email protected] 2 
相關問題