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手册。
传递一个结构型数组。结构定义如下:
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语言结构数组 动态录入游戏英雄信息并且计算平均生命值
- C语言多维数组参数传递问题
- C语言之使用指针数组传递参数
- C语言一级指针、二级指针、数组作为参数传递需要注意的若干问题
- C语言------数据结构(栈操作,数组实现)
- c语言一维数组做参数传递给函数:
- 数据结构之---C语言实现图的数组(邻接矩阵)存储表示
- c语言 数组名&&结构体名 为什么不能用数组名给数组赋值,结构体却可以
- 数据结构:数组的操作(C语言描述)
- C语言 数组的顺序表示与实现 数据结构
- C语言:两个顺序线性结构(数组)的并集
- C语言_向函数传递结构体信息_plusC14.5
- C语言中 数组作为函数形参传递相当于指针,在函数中不能得到数组长度,只能得到指针长度4
- [C语言]除非该数组为全局变量,否则只能在定义数组时获取数组长度,在传递过程中只是传递指针不能再次获取长度
- C语言 与结构体相关 函数 指针 数组
- c语言中用函数传递数组的两个方法
- 【ajax传递数组】从js到控制器后台,到mybatis,教你如何传递数组,项目中直接截图,springMVC结构
- (转载) C语言技巧:怎样把数组作为参数传递给函数?(值传递和指针传递)
- c语言一维数组做参数传递给函数:
- 数据结构基础(1)--数组C语言实现--动态内存分配(跟风追逐,加强突击)