gsoap创建webservice服务简单教程
版权声明:本文为博主原创文章,未经博主允许不得转载。
WebService、soap、gsoap
soap:简单对象访问协议,是一种轻量的、简单的、基于 XML 的协议,它被设计成在WEB
上交换结构化的和固化的信息。从这里的概念可以看得出来,soap是一个基于xml格式的web交互协议,而webservice是一种使用web方式实现的功能。就好像是网络视频服务器和http的关系,就是这里的webservice服务器和soap的关系。其实从历史上来说,先有的soap这种协议,然后微软用基于这种协议制作了webservice这种服务。
gsoap:是一种能够把C/C++语言的接口转换成基于soap协议的webservice服务的工具。
使用gsoap创建webservice服务
下载gsop

准备待导出的服务接口定义文件(比如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)
"stdio.h"
#include "soapH.h"
#include "gservice.nsmap"
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);
}
{
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;
}
如能看到如下信息,说明服务器运行正常:
Source Code
新建webservice客户端(gClient)
#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);
}
测试服务
启动服务器端gServer程序:
gsoap创建webservice服务简单教程的更多相关文章
- .NET C# 创建WebService服务简单的例子
Web service是一个基于可编程的web的应用程序,用于开发分布式的互操作的应用程序,也是一种web服务 WebService的特性有以下几点: 1.使用XML(标准通用标记语言)来作为数据交互 ...
- .NET创建WebService服务简单的例子
Web service是一个基于可编程的web的应用程序,用于开发分布式的互操作的应用程序,也是一种web服务 WebService的特性有以下几点: 1.使用XML(标准通用标记语言)来作为数据交互 ...
- webService服务简单实现
首先写一个简单的webservice服务 package com.service.impl; import java.util.Date; import javax.jws.WebService; i ...
- 根据wsdl文件用soapUi快速创建webService服务(有图有真相)
最近公司业务上使用webservice 频繁.由于之前都是自己搭建webservice 自己定义提供给别人服务,现在则相反需求都是根据人家提供的wsdl 文件来生成 我们平台需要提供的接口.刚开始不知 ...
- eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(二)
eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(二) 接上篇博客,本篇博客主要包含两个内容: 4.使用Android studio创建webservice客 ...
- eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(一)
eclipse使用CXF3.1.*创建webservice服务端客户端以及客户端手机APP(一) 本篇博客主要包含五个内容: 1.CXF换将搭建以及eclipse配置CXF. 2.eclipse创建w ...
- [转载]Java创建WebService服务及客户端实现
Java创建WebService服务及客户端实现 Java创建WebService服务及客户端实现
- VS2010使用c++、gSOAP创建WebService 图文教程
VS2010使用c++.gSOAP创建的WebService 图文教程 环境 操作系统:Windows 7gsoap版本:2.8.32C++编译器/开发环境:Visual Studio 2010 gS ...
- Java创建WebService服务及客户端实现(转)
简介 WebService是一种服务的提供方式,通过WebService,不同应用间相互间调用变的很方便,网络上有很多常用的WebService服务,如:http://developer.51cto. ...
随机推荐
- 元素类型为 "package" 的内容必须匹配 "(result-types?,interceptors?,default-interceptor-ref?
该错误为struts.xml内配置文件节点顺序错误. package内的元素节点必须按照以下顺序排放: result-types interceptors defau ...
- 关于Tarjan(2)
Tarjan有第二个神奇的用法,求强连通分量!!!!!!!!!!!!!!!!!!! 同样利用了dfn:dfs序,low:能回到的最早祖先的dfn: 废话少说 上板子 #include<iostr ...
- fastjson升级版本遇到的问题
前面的话: 有关阿里的fastjson升级时遇到的问题,链接如下 https://github.com/alibaba/fastjson/wiki/enable_autotype 我要说的,是我碰到这 ...
- flex中创建弹出窗口,并传值
在flex页面中首先创建一个弹出窗口,代码如下: <?xml version="1.0" encoding="utf-8"?> <s:Titl ...
- iOS开发之通过代码自定义一个控件
关于控件的继承关系(面试重点): (1)所有的控件都继承自UIView. (2)能监听事件的都是先继承自UIControl,UIControl再继承自UIView.比如UIButton. (3)能整体 ...
- SharePoint JavaScript 客户端对象使用视频教程
本次视频教程是为大家介绍如何使用SharePoint JavaScript客户端对象,包括对于站点.列表.文档库.列表项.文件夹.文件和附件等基本对象的操作,同时,为大家举几个简单的应用的例子,让大家 ...
- 【Yii系列】最佳实践之后台业务框架
缘起 上面的几章都讲概念了,没有怎么讲到实践的东西,可能会有些枯燥,这很正常的,概念还是需要慢慢啃的,尤其是官网其他的部分,需要狠狠的啃. 什么,你啃不动了?看看官网旁边的那个在线用户吧. 你不啃的时 ...
- 关于JavaScript中连等赋值那点事
今天看了前端网上关于JS的题目,其中有一道题目挺有意思的.如下 var a = b =10; (function(){ var a = b = 20; })(); console.log(a); co ...
- Oracle SQL 语言分类
Oracle SQL语句分类 2008-06-17 11:15:25 分类: Linux * 2008/06/17 星期二*蒙昭良*环境:WindowsXP + Oracle10gR2*Oracl ...
- Oracle常见错误集锦
1.ORA-12560:TNS:协议适配器错误 OracleService<SID>服务没有启动 2. ORA-12541:TNS:无监听程序 Oracle<ORACLE_HOME& ...