版权声明:本文为博主原创文章,未经博主允许不得转载。

WebService、soap、gsoap

WebService:就是一个应用程序,它向外界暴露出一个可以通过web进行调用的API,是分布式的服务组件。本质上就是要以标准的形式实现企业内外各个不同服务系统之间的互调和集成。
soap:简单对象访问协议,是一种轻量的、简单的、基于 XML 的协议,它被设计成在WEB
上交换结构化的和固化的信息。从这里的概念可以看得出来,soap是一个基于xml格式的web交互协议,而webservice是一种使用web方式实现的功能。就好像是网络视频服务器和http的关系,就是这里的webservice服务器和soap的关系。其实从历史上来说,先有的soap这种协议,然后微软用基于这种协议制作了webservice这种服务。
gsoap:是一种能够把C/C++语言的接口转换成基于soap协议的webservice服务的工具。

使用gsoap创建webservice服务

下载gsop

首先到http://sourceforge.net/projects/gsoap2/files/gSOAP/下载gsoap的最新版本。解压后目录结构如下:
 

我们看到bin\win32目录下有两个exe可执行文件:soapcpp2.exe,wsdl2h.exe。另外根目录还有两个比较重要的源文件:stdsoap2.h和stdsoap2.cpp。

准备待导出的服务接口定义文件(比如gservice.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);

新建webservice服务器端(gServer)

选择空控制台程序,并将soapcpp2.exe、stdsoap2.h、stdsoap2.cpp、gservice.h文件拷贝到gserver目下。启用cmd命令行程序,进入到gServer目录下执行如下命令:
soapcpp2.exe -S gservice.h
生成服务器端接口文件。并将stdsoap2.h、soapStub.h、soapH.h、gservice.nsmap和stdsoap2.cpp、soapC.cpp、soapServer.cpp文件导入到gServer中,项目目录结构如下:

实现main函数:
#include
"stdio.h"
#include "soapH.h"
#include "gservice.nsmap"
int
main(int argc,char **argv)
{
int nPort = 8080;
struct soap fun_soap;
soap_init(&fun_soap);
int nMaster = (int)soap_bind(&fun_soap, NULL, nPort, 100);
if (nMaster < 0)
{
soap_print_fault(&fun_soap, stderr);
exit(-1);
}
fprintf(stderr, "Socket connection successful : master socket = %d\n", nMaster);

while (true)
{
int nSlave = (int)soap_accept(&fun_soap);
if (nSlave < 0)
{
soap_print_fault(&fun_soap, stderr);
exit(-1);
}

fprintf(stderr, "Socket connection successful : slave socket = %d\n", nSlave);

soap_serve(&fun_soap);
soap_end(&fun_soap);
}

return 0;
}

/*加法的具体实现*/
int ns__add(struct soap *soap, int num1, int num2, int* result )   
{
if (NULL == result)
{
printf("Error:The third argument should not be NULL!\n");
return SOAP_ERR;
}
else
{
(*result) = num1 + num2;
return SOAP_OK;
}
return SOAP_OK;
}

/*减法的具体实现*/
int ns__sub(struct soap *soap,int num1, int num2, int* result )
{
if (NULL == result)
{
printf("Error:The third argument should not be NULL!\n");
return SOAP_ERR;
}
else
{
(*result) = num1 - num2;
return SOAP_OK;
}
return SOAP_OK;
}

/*乘法的具体实现*/
int ns__mult(struct soap *soap, int num1, int num2, int *result)
{
if (NULL == result)
{
printf("Error:The third argument should not be NULL!\n");
return SOAP_ERR;
}
else
{
(*result) = num1 * num2;
return SOAP_OK;
}
return SOAP_OK;
}

/*除法的具体实现*/
int ns__divid(struct soap *soap, int num1, int num2, int *result)
{
if (NULL == result || 0 == num2)
{
printf("Error:The second argument is 0 or The third argument is NULL!\n");
return SOAP_ERR;
}
else
{
(*result) = num1 / num2;
return SOAP_OK;
}
return SOAP_OK;
}
编译,运行服务器端程序,从浏览器访问localhost:8080
如能看到如下信息,说明服务器运行正常:
XML
Source Code


新建webservice客户端(gClient)

选择空控制台程序,并将soapcpp2.exe、stdsoap2.h、stdsoap2.cpp、gservice.h文件拷贝到gClient目下。启用cmd命令行程序,进入到gClient目录下执行如下命令:
soapcpp2.exe -C gservice.h
生成客户端代理接口文件。并将stdsoap2.h、soapStub.h、soapH.h、gservice.nsmap和stdsoap2.cpp、soapC.cpp、soapClient.cpp文件导入到gClient中,项目目录结构如下:
实现main函数:
#include "stdio.h"
#include "gservice.nsmap"

int main( int argc, char *argv[])
{
printf("The Client is runing...\n");
int num1 = 110;
int num2 = 11;
int result = 0;

struct soap *CalculateSoap = soap_new();
//soap_init(CalculateSoap);
char * server_addr = "http://localhost:8080";

int iRet = soap_call_ns__add(CalculateSoap,server_addr,"",num1,num2,&result);
if ( iRet == SOAP_ERR)
{
printf("Error while calling the soap_call_ns__add");
}
else
{
printf("Calling the soap_call_ns__add success。\n");
printf("%d + %d = %d\n",num1,num2,result);
}

iRet = soap_call_ns__sub(CalculateSoap,server_addr,"",num1,num2,&result);
if ( iRet == SOAP_ERR)
{
printf("Error while calling the soap_call_ns__sub");
}
else
{
printf("Calling the soap_call_ns__sub success。\n");
printf("%d - %d = %d\n",num1,num2,result);
}

iRet = soap_call_ns__mult(CalculateSoap,server_addr,"",num1,num2,&result);
if ( iRet == SOAP_ERR)
{
printf("Error while calling the soap_call_ns__mult");
}
else
{
printf("Calling the soap_call_ns__mult success。\n");
printf("%d * %d = %d\n",num1,num2,result);
}

iRet = soap_call_ns__divid(CalculateSoap,server_addr,"",num1,num2,&result);
if ( iRet == SOAP_ERR)
{
printf("Error while calling the soap_call_ns__divid");
}
else
{
printf("Calling the soap_call_ns__divid success。\n");
printf("%d / %d = %d\n",num1,num2,result);
}
soap_end(CalculateSoap);
soap_done(CalculateSoap);
free(CalculateSoap);

return 0;
}

测试服务

启动服务器端gServer程序:

启动客户端gClient程序:

 

gsoap创建webservice服务简单教程的更多相关文章

  1. .NET C# 创建WebService服务简单的例子

    Web service是一个基于可编程的web的应用程序,用于开发分布式的互操作的应用程序,也是一种web服务 WebService的特性有以下几点: 1.使用XML(标准通用标记语言)来作为数据交互 ...

  2. .NET创建WebService服务简单的例子

    Web service是一个基于可编程的web的应用程序,用于开发分布式的互操作的应用程序,也是一种web服务 WebService的特性有以下几点: 1.使用XML(标准通用标记语言)来作为数据交互 ...

  3. webService服务简单实现

    首先写一个简单的webservice服务 package com.service.impl; import java.util.Date; import javax.jws.WebService; i ...

  4. 根据wsdl文件用soapUi快速创建webService服务(有图有真相)

    最近公司业务上使用webservice 频繁.由于之前都是自己搭建webservice 自己定义提供给别人服务,现在则相反需求都是根据人家提供的wsdl 文件来生成 我们平台需要提供的接口.刚开始不知 ...

  5. eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(二)

    eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(二) 接上篇博客,本篇博客主要包含两个内容: 4.使用Android studio创建webservice客 ...

  6. eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(一)

    eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(一) 本篇博客主要包含五个内容: 1.CXF换将搭建以及eclipse配置CXF. 2.eclipse创建w ...

  7. [转载]Java创建WebService服务及客户端实现

    Java创建WebService服务及客户端实现 Java创建WebService服务及客户端实现

  8. VS2010使用c++、gSOAP创建WebService 图文教程

    VS2010使用c++.gSOAP创建的WebService 图文教程 环境 操作系统:Windows 7gsoap版本:2.8.32C++编译器/开发环境:Visual Studio 2010 gS ...

  9. Java创建WebService服务及客户端实现(转)

    简介 WebService是一种服务的提供方式,通过WebService,不同应用间相互间调用变的很方便,网络上有很多常用的WebService服务,如:http://developer.51cto. ...

随机推荐

  1. JavaScript 基础阶段测试题

    JavaScript 基础阶段测试题,试试你能得多少分? 一.选择题1.分析下段代码输出结果是( )    var arr = [2,3,4,5,6];    var sum =0;    for(v ...

  2. paoding-rose 了解

    paoding-rose 是人人开源的基于 spring 开发的 javaEE 框架.wiki 地址: https://code.google.com/archive/p/paoding-rose/ ...

  3. idea调试SpringMvc, 出现:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener错误的解决办法

    有时,使用idea开发SpringMvc发现调试时出现以下错误: 12-Mar-2017 12:08:02.345 严重 [RMI TCP Connection(2)-127.0.0.1] org.a ...

  4. GCC命令

    一. 常用编译命令选项 源程序test.c 1. 无选项编译链接用法:#gcc test.c作用:将test.c预处理.汇编.编译并链接形成可执行文件.这里未指定输出文件,默认输出为a.out. 2. ...

  5. ISO c++ 14 重点介绍[译]

    原文链接 http://marknelson.us/2014/09/11/highlights-of-iso-c14/ 下面是对你的日常开发有重大影响的C++14新变动,列出了一些示例代码,并讨论何时 ...

  6. node.js平台下Express的session与cookie模块包的配置

    首先下载两个模块包 session模块包:用于保持登录状态或保持会话状态等. npm install express-session --save-dev cookie模块包:用于解析cookie. ...

  7. waypoints

    http://imakewebthings.com/waypoints waypoints 滑冰122分钟 Cygwin http:/nxutils.sourceforge.net http://ba ...

  8. ceph集群安装

    所有 Ceph 部署都始于 Ceph 存储集群.一个 Ceph 集群可以包含数千个存储节点,最简系统至少需要一个监视器和两个 OSD 才能做到数据复制.Ceph 文件系统. Ceph 对象存储.和 C ...

  9. cassandra高级操作之分页的java实现(有项目具体需求)

    接着上篇博客,我们来谈谈java操作cassandra分页,需要注意的是这个分页与我们平时所做的页面分页是不同的,具体有啥不同,大家耐着性子往下看. 上篇博客讲到了cassandra的分页,相信大家会 ...

  10. ElasticSearch Index API && Mapping

    ElasticSearch  NEST Client 操作Index var indexName="twitter"; var deleteIndexResponse = clie ...