CPP-网络/通信:gsoap 的教程和使用

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 t the "Link" tab (the project file needs to be selected in the file view) an d 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 包中的帮助文档,本文档略。

转载于:https://www.cnblogs.com/CPYER/p/3387368.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
带gsoap-2.8源码,基于服务器客户端的实例,带自动生成服务客户端代码的批处理程序,及如何使用。带自己学习参考的教程; 0.解压附件,soapInterface.bat所在路径不得含中文 空格 1.新建头文件 取soapInterface.bat文件的同名:soapInterface.h 文件内编写接口,具体说明看附件参考的教程 //gsoap ns service name: gservice //gsoap ns service style: rpc int ns__add(int num1, int num2, int* result ); int ns__sub(int num1, int num2, int* result ); int ns__mult( int num1, int num2, int *result); int ns__divid( int num1, int num2, int *result); 2.从附件内gsoap-2.8包中搜索复制stdsoap2.h,stdsoap2.cppsoapcpp2.exe, 存放于soapInterface.bat同级目录 3.双击soapInterface.bat运行。生成gClientSoap,gServerSoap两个文件夹,分别复制到服务器工程与客户端工程中使用 4.gClientSoap,gServerSoap两个文件夹内用到的文件功能说明,更多参考附件教程 1)soapC.cpp , soapH.h//soap的序列和反序列代码,它已经包含了soapStub.h 2)soapServer.c ppsoapServerLib.cpp //服务器端代码(纯C代码是soapServer.c soapServerLib.c ),soapServerLib.cpp文件则只是简单地包含soapServer.cppsoapC.cpp 3)soapClient.cpp soapClientLib.cpp//客户端代码(纯C代码是soapClient.csoapClientLib.c ),soapClientLib.cpp文件则只是简单地包含soapClient.cppsoapC.cpp 4) soapStub.h // soap的存根文件,定义了我们编写的头文件里对应的远程调用模型 5) add.nsmap //XML服务命名空间 6)服务器端要载入的文件有:soapServer.cpp,soapC.cpp,stdsoap2.cpp; 要包含的文件有:gservice.nsmap,soapH.h; 客户端要输入的文件有: soapClient.cppsoapC.cpp,stdsoap2.cpp; 要包含的文件有:gservice.nsmap,soapH.h

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值