好记性不如铅笔头

编程, 网络通讯

SOAP简单学习和使用笔记

CONTENTS

SOAP协议简单介绍

SOAP(原为Simple Object Access Protocol的首字母缩写,即简单对象访问协议)是交换数据的一种协议规范,使用在计算机网络Web服务(web service)中,交换带结构的信息。SOAP为了简化网页服务器(Web Server)从XML数据库中提取数据时,节省去格式化页面时间,以及不同应用程序之间按照HTTP通信协议,遵从XML格式执行资料互换,使其抽象于语言实现、平台和硬件。

SOAP封装(envelope):定义了一个框架,描述消息中的内容是什么、是谁发送的、谁应当接受并处理它以及如何处理它们;
SOAP编码规则(encoding rules):定义了一种序列化的机制,用于表示应用程序需要使用的数据类型的实例;
SOAP RPC表示(RPC representation):定义了一个协定,用于表示远程过程调用和应答;
SOAP绑定(binding),它定义了SOAP使用哪种协议交换信息。使用HTTP/TCP/UDP协议都可以。

gSOAP介绍

gSOAP的编译器能够自动的将用户定义的本地化的C或C++数据类型转变为符合XML语法的数据结构,反之亦然。这样,只用一组简单的API就将用户从SOAP细节实现工作中解脱了出来,可以专注与应用程序逻辑的实现工作了。gSOAP编译器可以集成C/C++和Fortran代码(通过一个Fortran到C的接口),嵌入式系统,其他SOAP程序提供的实时软件的资源和信息;可以跨越多个操作系统,语言环境以及在防火墙后的不同组织。

官网:【 https://www.genivia.com/dev.html
官方下载地址:【 https://www.genivia.com/downloads.html
tutorials:【 https://www.genivia.com/tutorials.html

gSOAP使用笔记

下载安装

下载最新版本gSOAP压缩包,解压。

Windows(MSVC)安装方式

windows下已经编译好了可执行文件,如下:

gsoap/bin/win32/wsdl2h.exe translator of WSDL/XSD to services and XML data bindings (interface tool)
gsoap/bin/win64/wsdl2h.exe translator of WSDL/XSD to services and XML data bindings (interface tool)
gsoap/bin/win32/soapcpp2.exe code generator for services and XML data bindings (implementation tool)
gsoap/bin/win64/soapcpp2.exe code generator for services and XML data bindings (implementation tool)

Linux安装方式

#安装依赖
sudo apt-get install flex bison libssl-dev

#解压缩zip包
unzip -q gsoap_2.8.111.zip

#编译安装
./configure
make
sudo make install

官方demo演示

参考链接
https://www.genivia.com/examples/calc/index.html
https://www.genivia.com/dev.html#client-c

初始.h文件

#首先根据wsdl生成初始.h文件
wsdl2h -c -o calc.h http://www.genivia.com/calc.wsdl

client端

client侧代码生成

soapcpp2 -c -CL calc.h

新建文件test_client.c,内容如下:

#include "calc.nsmap"
#include "soapH.h"

int main()
{
  struct soap *soap = soap_new();
  double sum;
  if (soap_call_ns2__add(soap, NULL, NULL, 1.23, 4.56, &sum) == SOAP_OK)
    printf("Sum = %g\n", sum);
  else
    soap_print_fault(soap, stderr);
  soap_destroy(soap); // delete managed deserialized C++ instances
  soap_end(soap);     // delete other managed data
  soap_free(soap);    // free the soap struct context data
}

编译生成可执行文件

gcc -o test_client test_client.c soapC.c soapClient.c  ../gsoap-2.8/gsoap/stdsoap2.c

执行后可以看到结果和抓包,抓包截图如下:

server端

server侧代码生成

soapcpp2 -c  -SL calc.h

新建文件test_server.c,内容如下:

#include "soapH.h"
#include "calc.nsmap"

int port = 8080;

int main()
{
  struct soap *soap = soap_new();
  if (!soap_valid_socket(soap_bind(soap, NULL, port, 10)))
    exit(EXIT_FAILURE);
  while (soap_valid_socket(soap_accept(soap)))
  {
    if (soap_serve(soap) != SOAP_OK)
      break;
    soap_destroy(soap);
    soap_end(soap);
  }
  soap_print_fault(soap, stderr);
  soap_free(soap);
  return 0;
}

/* service operation function implementation */
int ns2__add(struct soap *soap, double a, double b, double *result)
{
  *result = a + b;
  return SOAP_OK;
} 
/* service operation function implementation */
int ns2__sub(struct soap *soap, double a, double b, double *result)
{
    return soap_sender_fault(soap, "NOT Support!!", NULL);  
} 
/* service operation function implementation */
int ns2__mul(struct soap *soap, double a, double b, double *result)
{
    return soap_sender_fault(soap, "NOT Support!!", NULL);  
} 
/* service operation function implementation */
int ns2__div(struct soap *soap, double a, double b, double *result)
{
    return soap_sender_fault(soap, "NOT Support!!", NULL);
} 
/* service operation function implementation */
int ns2__pow(struct soap *soap, double a, double b, double *result)
{
    return soap_sender_fault(soap, "NOT Support!!", NULL);
}

编译生成可执行文件

gcc -o test_server test_server.c soapC.c soapServer.c ../gsoap-2.8/gsoap/stdsoap2.c

修改下test_client.c,改为本地server调用,如下图:

重新编译,分别执行test_server,test_client 就可以看到效果了。

发表评论

19 − 17 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据