http://yangzb.iteye.com/blog/422463
1.1.1 gSOAP
1.1.1.1 简介
gSOAP编译工具提供了一个SOAP/XML 关于C/C++ 语言的实现,从而让C/C++语言研发web服务或客户端程式的工作变得轻松了很多。绝大多数的C++web服务工具包提供一组API函数类库来处理特定的SOAP数据结构,这样就使得用户必须改变程式结构来适应相关的类库。和之相反,gSOAP利用编译器技术提供了一组透明化的SOAP API,并将和研发无关的SOAP实现细节相关的内容对用户隐藏起来。gSOAP的编译器能够自动的将用户定义的本地化的C或C++数据类型转变为符合XML语法的数据结构,反之亦然。这样,只用一组简单的API就将用户从SOAP细节实现工作中解脱了出来,能够专注和应用程式逻辑的实现工作了。gSOAP编译器能够集成C/C++和Fortran代码(通过一个Fortran到C的接口),嵌入式系统,其他SOAP程式提供的实时软件的资源和信息;能够跨越多个操作系统,语言环境连同在防火墙后的不同组织。
gSOAP使编写web服务的工作最小化了。gSOAP编译器生成SOAP的代码来序列化或反序列化C/C++的数据结构。gSOAP包含一个WSDL生成器,用他来为您的web服务生成web服务的解释。gSOAP的解释器及导入器能够使用户无需分析web服务的细节就能够实现一个客户端或服务端程式。
1.1.1.2 gSOAP+VC研发客户端
gSOAP是开放的C/C++源码的soap服务器实现,本章节简单介绍使用gSOAP研发2.2.1.3中的AXIS服务器的客户程式。
下载gSOAP工具的代码地址,当前最新版本是2.7.8c版本:
http://sourceforge.net/project/showfiles.php?group_id=52781
解压缩本地目录,进入bin目录
根据wsdl生成头文档方式有以下几种:
生成C++代码
$ wsdl2h -o testClient.h http://localhost:8080/axis/services/HelloService?wsdl
生成C++代码,不是用STL
$ wsdl2h -s -o testClient.h http://localhost:8080/axis/services/HelloService?wsdl
生成纯C代码
$ wsdl2h -c -o testClient.h http://localhost:8080/axis/services/HelloService?wsdl
本例使用C++代码(含STL)方式
生成客户端代码,使用如下命令(不带-C参数生成客户端和服务器代码)
soapcpp2 -C –I..\import testClient.h
打开VC,创建一个Win32的控制台程式soapClient,"Project", Settings", select the "Link" tab (the project file needs to be selected in the file view) and add "wsock32.lib。
把刚刚生成的以下代码添加到工程中去:
soapStub.h soapH.h soapC.cpp soapClient.cpp soapClientLib.cpp
在main函数中写入代码soapClient.cpp:
// soap.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "soapH.h"
#include "HelloServiceSoapBinding.nsmap"
int main(int argc, char* argv[])
{
struct soap soap;
std::string a="STS Corp.";
int b=0;
std::string result;
if (argc < 3)
{ fprintf(stderr, "Usage: string num\n");
exit(0);
}
soap_init(&soap);
a=argv[1];
b = atoi(argv[2]);
DWORD begin= GetTickCount();
for (int i=0;i<1;i++)
{
soap_call_ns1__sayHello(&soap, "http://10.41.25.70:8080/axis/services/HelloService", "", a, b, result);
if (soap.error)
{
soap_print_fault(&soap, stderr);
exit(1);
}
else
printf("result = %s\n", result.c_str());
b++;
}
DWORD end= GetTickCount();
printf("每秒处理%d个\n", 1*1000/(end-begin));
soap_destroy(&soap);
soap_end(&soap);
soap_done(&soap);
system("pause");
return 0;
}
编译完成后,直接以命令行方式运行程式soapClient “STS Corp.” 9
输出结果为result = Hello:“STS~~~~~~~~~~square=0,配置循环控制变量能够简单用于测试性能。
代码中使用soap_init2能够配置http1.1消息头的connection属性为keep-alive,当配置为keep-alive时,每秒交互能够达到360次以上,假如使用close方式,每秒交互大约200次以上。
发出的请求消息如下:
POST /axis/services/HelloService HTTP/1.1
Host: 10.41.25.70:8080
User-Agent: gSOAP/2.7
Content-Type: text/xml; charset=utf-8
Content-Length: 478
Connection: close
SOAPAction: ""
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelopexmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:ns1="http://demo"><SOAP-ENV:BodySOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><ns1:sayHello><in0>STSCorp.</in0><in1>9</in1></ns1:sayHello></SOAP-ENV:Body></SOAP-ENV:Envelope>
响应消息:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/xml;charset=utf-8
Date: Tue, 29 Aug 2006 10:36:45 GMT
Connection: close
<?xmlversion="1.0" encoding="utf-8"?><soapenv:Envelopexmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><ns1:sayHelloResponsesoapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"xmlns:ns1="http://demo"><sayHelloReturnxsi:type="xsd:string">Hello:STSCorp.~~~~~~~~~~square=81</sayHelloReturn></ns1:sayHelloResponse></soapenv:Body></soapenv:Envelope>
1.1.1.3 gSOAP+VC研发服务器
gSOAP是开放的C/C++源码的soap服务器实现,能够参考gSOAP包中的帮助文档,本文档略。