簡體   English   中英

如何在onc-rpc中將字符串(char *)從服務器發送到客戶端

[英]How te send a string (char*) from server to client in onc-rpc

我希望有人能幫助我。 我正在制作一個程序,該程序將一個長變量從客戶端發送到服務器,最后一個必須以字符串響應。 我想指出我正在使用onc-rpc框架(如果我沒有記錯的話,請使用sunRPC)。

這是我當前的頭文件=> msg.x

//msg.x
program MESSAGEPROG
{
    version MESSAGEVERS
    {
        string FIBCALC(long) = 1;
     } = 1;
} = 0x20000001;

我的服務器存根必須實現此功能。 我不會放置所有代碼,因為這是一項作業。

我的服務器存根=> server.c

#include <rpc/rpc.h>
#include <stdio.h>
#include <stdlb.h>
#include "msg.h"

char ** fibcalc_1_svc(whatToUse, dummy)
    long *whatToUse;
    struct svc_req *dummy;
{
    char whatToSend;

    whatToSend = (char **)malloc(sizeof(char*));
    *whatToSend = (char *)malloc(sizeof(char) * STRING_SIZE);

    //............

    return whatToSend;
}

不用說,其余實現無需rpc即可工作。 如果我打印的字符串,它適用於非rpc C文件。

#include <rpc/rpc.h>
#include <stdio.h>
#include <stdlb.h>
#include "msg.h"

int main(int argc, char *argv[])
{
CLIENT *cl;
char **result;
long *whatToSend, *test;
FILE *fout, *fin;


whatToSend = (long *)malloc(sizeof(long));
result = (char **)malloc(sizeof(char*));
*result = (char *)malloc(sizeof(char) * STRING_SIZE);

if(argc != 3)
{
    /* if arguments are not passed corectly
     * we print the following message and close with exit error
     */
    fprintf(stderr, "usage : ./%s [server ip] [fileIn]\n", argv[0]);
    exit(1);
}

cl = clnt_create(argv[1], 
                 MESSAGEPROG, 
                 MESSAGEVERS, 
                 "tcp");
if(cl == NULL)
{
    /* if no connection to server
     * we print the following message and close with exit error
     */
    clnt_pcreateerror(argv[1]);
    exit(1);
}


/* Sanity checks for file handle
 */
fin = fopen(argv[2],"r");
if (fin == NULL)
{
    fprintf(stderr, "Input handle could not be opened!\n");
    exit(1);
}   

fout = fopen("out.txt", "w");
if (fout == NULL)
{
    fprintf(stderr, "Output handle could not be opened!\n");
    exit(1);
}


while(fscanf(fin, "%ld", whatToSend) != EOF)
{
    memset(*result, 0, STRING_SIZE);



    result = fibcalc_1(whatToSend, cl);

    if(result == NULL)
    {
        /* Server did not respond
         */
        clnt_pcreateerror("localhost");
        exit(1);
    }

    printf("%s\n", *result);
}

/* Sanity checks for closing the handles
 */
if(fclose(fin)) 
{
    fprintf(stderr, "Input handle could not be closed!!\n");
    exit(1);
}

if(fclose(fout)) 
{
    fprintf(stderr, "Output handle could not be closed!!\n");
    exit(1);
}

/* Free allocated memory
 */
free(whatToSend);
free(*result);
free(result);

exit(0);
}

當我收到服務器消息時,出現段錯誤。 當我gdb時,並在以下位置執行客戶端程序

result = fibcalc_1(whatToSend, cl); 

我得到結果地址是0x00

當我將結果類型更改為int或long或w / e時,結果很好地顯示出來並且程序可以運行。

我還想指出,結果是char **類型,因為在onc-rpc中字符串是char *類型,並且我開始意識到服務器功能必須返回的任何變量都是返回值的地址。

我希望我能很好地解釋我的問題。 我的第一個想法是,在服務器功能中,char whatToSend [20]的類型應為char *,我應該對其進行分配,但是我應如何對其進行解除分配?

先感謝您。

我的問題是,當我嘗試從服務器存根函數發送結果時,我沒有意識到我發送的內容必須保存在.data(靜態聲明)或heap(malloc)上。 我的解決辦法是更改服務器存根中的以下內容。

char ** fibcalc_1_svc(whatToUse, dummy)
     long *whatToUse;
     struct svc_req *dummy;
{
     char whatToSend;

     whatToSend = (char **)malloc(sizeof(char*));
     *whatToSend = (char *)malloc(sizeof(char) * STRING_SIZE);

     //............

     return whatToSend;
}

在客戶端中,我嘗試在函數調用后釋放結果。 雖然我有內存泄漏,但現在可以使用。 謝謝@chux的幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM