`
yangzb
  • 浏览: 3478012 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

gsoap 的教程和使用

    博客分类:
  • C++
阅读更多

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.8 c 版本:

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", selec the "Link" tab (the project file needs to be selected in the file view) an 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:Envelope xmlns: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:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><ns1:sayHello><in0>STS Corp.</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

<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns: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:sayHelloResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://demo"><sayHelloReturn xsi:type="xsd:string">Hello:STS Corp.~~~~~~~~~~square=81</sayHelloReturn></ns1:sayHelloResponse></soapenv:Body></soapenv:Envelope>

1.1.1 .3      gSOAP+VC 研发服务器

gSOAP 是开放的 C/C++ 源码的 soap 服务器实现,能够参考 gSOAP 包中的帮助文档,本文档略。

 

分享到:
评论
1 楼 charles_gz 2011-10-09  
新手,首先学习了!有个问题请教一下。文章中所说的服务器是已经存在的、能不能手动写服务器呢?期待您的回答!

相关推荐

Global site tag (gtag.js) - Google Analytics