【问题标题】:sending a struct array with sun rpc from server to client从服务器向客户端发送带有 sun rpc 的结构数组
【发布时间】:2015-11-13 23:54:04
【问题描述】:

如何在 ansi-c sun-rpc 中正确地将结构从服务器发送到客户端?

在我的 test.x IDL 文件中,我定义了一个带有字符串和 int 的结构集群 和一个类型 clusters,它是一个可变长度的簇元素数组:

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。这似乎是某种内存管理不善,我也不确定我是否正确处理了服务器端的序列化。

我刚刚用 test.h(由 rpcgen)中定义的适当值填充了 result.clusters_len 和 result.clusters_val:

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

我是否必须在服务器端使用 xdr_clusters 才能正确序列化结果?

谢谢

【问题讨论】:

    标签: c arrays rpc ansi-c sunrpc


    【解决方案1】:

    好的,我想出了我的错误,让我们总结一下:

    • 知道如何正确初始化一个 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: %s@runlevel %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: cl1@runlevel 1
    cluster #1: cl2@runlevel 2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-07
      • 1970-01-01
      • 1970-01-01
      • 2015-02-10
      • 2017-01-12
      • 1970-01-01
      • 2015-06-20
      相关资源
      最近更新 更多