【发布时间】: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