首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >te如何在onc-rpc中将字符串(char*)从服务器发送到客户端

te如何在onc-rpc中将字符串(char*)从服务器发送到客户端
EN

Stack Overflow用户
提问于 2014-10-29 01:02:42
回答 1查看 3.3K关注 0票数 1

我希望有人能帮我。我正在制作一个程序,它将一个长变量从客户端发送到服务器,最后一个必须用字符串进行响应。我想指出,我正在使用onc框架(如果我没有弄错的话,可以使用sunRPC)。

这是我当前的标题=> msg.x

代码语言:javascript
复制
//msg.x
program MESSAGEPROG
{
    version MESSAGEVERS
    {
        string FIBCALC(long) = 1;
     } = 1;
} = 0x20000001;

我的服务器存根必须实现此功能。我不会把所有的代码都放下来,因为这是家庭作业的辅助。

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

代码语言:javascript
复制
#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;
}

不用说,rest实现在没有rpc的情况下工作。如果我打印该字符串,它将在一个非rpc C文件上工作。

代码语言:javascript
复制
#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);
}

当我收到服务器消息时,我会出现seg错误。当我的gdb,并步进客户端程序

代码语言:javascript
复制
result = fibcalc_1(whatToSend, cl); 

我知道结果地址是0x00

当我将结果类型更改为int或long或w/e时,结果很好,程序运行良好。

我还想指出,结果是char**类型,因为字符串在onc-rpc中是char *类型,我意识到服务器函数必须返回的任何变量都是返回值的地址。

我希望我能解释清楚我的问题。我的第一个想法是,在服务器函数中,char whatToSend20应该是char *类型,我应该分配它,但是我如何分配它呢?

提前谢谢你。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-31 14:36:29

我的问题是,当我试图从服务器存根函数发送结果时,我没有意识到我发送的内容必须保存在.data(静态声明)或堆(Malloc)上。我的决心是在服务器存根中更改以下内容。

代码语言:javascript
复制
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的帮助

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26621169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档