您的位置:首页 > 编程语言 > C语言/C++

C语言用gsoap传递结构型数组

2011-02-14 13:16 148 查看
折腾了近一周时间,终于搞出来在C中用gsoap传递结构型数组了。用了一堆指针,好麻烦哈!

传递一个结构型数组。结构定义如下:

struct JobStatusDetail {

int jobId;

int status;

char cpuTime[10];

char wallTime[10];

}

步骤一、定义gsoap的.h文件

//gsoap ns service name: JobService

//gsoap ns service style: rpc

//gsoap ns service encoding: encoded

//gsoap ns service namespace: http://abc.com/Jobservice.wsdl

//gsoap ns service location: http://abc.com/Jobservice.cgi

//gsoap ns schema namespace: urn:JobService

typedef struct JobStatusDetail {

int jobId;

int status;

char cpuTime[10];

char wallTime[10];

} testType;

typedef testType *xsd__JobStatusDetailPointer;

struct ns__JobStatusPointer {

xsd__JobStatusDetailPointer *__ptr;

int __size;

};

int ns__StructArrayTest(char *cmd, struct ns__JobStatusPointer *structResult);

步骤二、实现服务器端的函数如下,整个gsoap服务器端程序结构可参见gsoap服务器与客户机程序的建立过程

int ns__StructArrayTest(struct soap *soap, char *cmd, struct ns__JobStatusPointer *structResult) {

structResult->__size=2; //数组有两个element

structResult->__ptr = malloc((structResult->__size+1)*sizeof(*structResult->__ptr));

structResult->__ptr[0]=(testType *)malloc(sizeof(testType));

(*structResult->__ptr[0]).jobId=1; //搞了很久,终于明白(*structResult->__ptr[0])里一定存放结构具体内容

(*structResult->__ptr[0]).status=1;

strcpy((*structResult->__ptr[0]).cpuTime,"11:11:11");

strcpy((*structResult->__ptr[0]).wallTime,"22:22:22");

structResult->__ptr[1]=(testType *)malloc(sizeof(testType));

(*structResult->__ptr[1]).jobId=2;

(*structResult->__ptr[1]).status=2;

strcpy((*structResult->__ptr[1]).cpuTime,"33:33:33");

strcpy((*structResult->__ptr[1]).wallTime,"44:44:44");

return SOAP_OK;

}

步骤三、实现客户端程序

#include "soapH.h"

#include "JobService.nsmap"

#include <string.h>

const char server[] = "http://**.**.**:nnnn";

static char Server[]="**.**.**";

int main(int argc, char **argv)

{

struct soap soap;

char buf[1024]="Tell me the job status!";

struct ns__JobStatusPointer result;

if (argc<1)

{

fprintf(stderr, "Usage: jobOptionfile \n");

exit(0);

}

soap_init(&soap);

strcpy(buf, argv[1]);

soap_call_ns__StructArrayTest(&soap, server,"", buf, &result);

if (soap.error) {

soap_print_fault(&soap, stderr);

printf("soap wrong: %d, \n please inform the administrator!\n",soap.error);

exit(1);

}

int i;

for (i=0; i<result.__size; i++)

printf ("jobid: %d jobStatus:%d, cpuTime:%s, wallTime:%s\n",result.__ptr[i]->jobId,result.__ptr[i]->status,result.__ptr[i]->cpuTime, result.__ptr[i]->wallTime);

soap_destroy(&soap);

soap_end(&soap);

return 1;

}

编译完成后,运行客户端程序,在运行结束后,服务器和客户端的当前目录下会有SENT.log和RECV.log两个文件,可以用来查来两端收发消息的内容。

gsoap一次可以传递的信息的大小是有缺省规定的,但可以修改。可查看gsoap手册。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C语言 数组 休闲