1.编写头文件cal.h:

//gsoap ns service name: calc
//gsoap ns service style: rpc
//gsoap ns service encoding: encoded
//gsoap ns service namespace: http://127.0.0.1:8089/calc.wsdl
//gsoap ns service location: http://127.0.0.1:8089/cal
//gsoap ns schema namespace: urn:calc
int ns__add(double a, double b, double *result);
int ns__sub(double a, double b, double *result);
int ns__mul(double a, double b, double *result);
int ns__div(double a, double b, double *result);
int ns__pow(double a, double b, double *result);

2.生成文件:

soapcpp2.exe -i cal.h

3.将以下文件拷入项目:

4.编写gSOAPService.cpp:

\#define _CRT_SECURE_NO_WARNINGS   **//一定要添加上**
\#include "calc.nsmap"
\#include"soapcalcService.h"
\#include "iostream" //控件问提只能写“” using namespace std; //很重要
int http_get(struct soap *soap)
{
FILE*fd = NULL;
fd = fopen("C:\\Users\\Hunter\\Documents\\Visual Studio 2015\\Projects\\Service\\Debug\\calc.wsdl", "rb"); //open WSDL file to copy if (!fd)
{
return 404; //return HTTP not found error
}
soap->http_content = "text/xml"; //HTTP header with text /xml content
soap_response(soap, SOAP_FILE);
for (;;)
{
size_t r = fread(soap->tmpbuf, 1, sizeof(soap->tmpbuf), fd);
if (!r)
{
break;
}
if (soap_send_raw(soap, soap->tmpbuf, r))
{
break; //cannot send, but little we can do about that
}
}
fclose(fd);
soap_end_send(soap);
return SOAP_OK;
} int main(int argc, char *argv[])
{
calcService cal;
cal.fget = http_get;
while (1)
{
if (cal.run(8089))
{
cal.soap_stream_fault(std::cerr);
}
}
return 0;
} //自动生成了calcService类,自己重写add等函数
/*加法的具体实现*/
int calcService::add(double num1, double num2, double* 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 calcService::sub(double num1, double num2, double* 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 calcService::mul(double num1, double num2, double* 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 calcService::div(double num1, double num2, double* result)
{
if (NULL == result || 0 == num2)
{
return soap_senderfault("Square root of negative value", "I can only compute the square root of a non-negative value");
return SOAP_ERR;
}
else
{
(*result) = num1 / num2;
return SOAP_OK;
}
return SOAP_OK;
} int calcService::pow(double num1, double num2, double* 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;
}

测试一:

http://127.0.0.1:8089/calc.wsdl

测试二:

运行服务器程序,并执行:

wsdl2h.exe -s -o cal.h http://127.0.0.1:8089/calc.wsdl
soapcpp2.exe -i cal.h



stdsoap2在gSOAP目录中搜索。

test.cpp:

\#include"soapcalcProxy.h"
\#include"calc.nsmap"
\#include"iostream" using namespace std; int main()
{
calcProxy p;
double result = 0;
if (SOAP_OK == p.add(1, 2, result))
{
cout << result << endl;
}
else
{
p.soap_stream_fault(std::cerr);
}
system("pause");
return 0;
}

gSOAP:C++编写服务器端的更多相关文章

  1. 使用Qt编写服务器端程序(包括Http传输服务器端)的方法

    使用Qt编写客户端的程序的示例或demo较多,但是编写服务器端程序的demo很少.当然,服务器端的程序一般不需要带界面,这点我们可以理解.不过有些时候我们还是需要使用Qt编写一个简单的测试用的服务器代 ...

  2. [转贴]gsoap使用心得!

    最近换了个工作环境,现在在大望路这边上班,呵,刚上班接到的任务就是熟悉gsoap!废话少说,现在开始gSoap学习! gSOAP是一个夸平台的,用于开发Web Service服务端和客户端的工具,在W ...

  3. vc++2008 采用GSoap访问 WebService

    (转http://www.cppblog.com/yeqing/articles/12762.html) 前一阶段写gSOAP 的文章没保存好,后来想写的,越学越没有写的勇气了,感觉自己很菜,但是现在 ...

  4. nodejs 编写(添加时间戳)命令行工具 timestamp

    Nodejs除了编写服务器端程序还可以编写命令行工具,如gulp.js就是Nodejs编写的. 接下来我们来实现一个添加时间戳的命令: $ timestamp action https://www.n ...

  5. Socket编程——客户端,服务器端的读写操作

    URL网络编程,最大的特征就是一对一的响应! 1:客户端“写”,服务器端用于“读” package coreBookSocket2; import java.io.InputStreamReader; ...

  6. Android与服务器端数据交互(http协议整合struts2+android)

    在android中有时候我们不需要用到本机的SQLite数据库提供数据,更多的时候是从网络上获取数据,那么Android怎么从服务器端获取数据呢?有很多种,归纳起来有 一:基于Http协议获取数据方法 ...

  7. 入职第一天:前端leader手把手教我入门Vue服务器端渲染(SSR)

    继前段时间西安电面之后顺利拿到了OFFER,今天(5月2号)是我入职第一天,在简短的内部培训了一上午后,前端leader让我先了解下什么是vue的服务器端渲染(SSR). SSR,英文全称叫 Serv ...

  8. 服务器端网络编程之 IO 模型

    引言  从 T 跳槽到 A 之后,我的编程语言也从 C++ 转为 了 Java.在 T 做的偏服务器端开发,而在 A 更偏向于业务开发.上周在 A 公司组内做了一个<服务器端高性能网络编程> ...

  9. gSOAP 初体验

    安装 由于本人使用的是 Mac OS 系统,故以 Mac OS 为例说明如何安装 gSOAP. 1)下载 gSOAP 可以在 https://sourceforge.net/projects/gsoa ...

随机推荐

  1. BZOJ 4569 【SCOI2016】 萌萌哒

    题目链接:萌萌哒 我先不吐槽题目名……这道题的并查集好像我们考过……既然那道题我没写就来把这道题写了吧(雾 这道题由于合并操作只有\(m\)次,那么很显然的一个想法就是把建一棵线段树类似物,然后每次在 ...

  2. WPF特效和例子

    https://www.cnblogs.com/AaronYang/p/4710428.html

  3. SCWS中文分词PHP扩展详细安装说明

    因最近写的一段代码,需要用到中文分词,在网上找了一下,发现了scws这个不错的插件,故根据文档安装使用,下面记录下安装的全过程 系统:centos 安装scws wget http://www.xun ...

  4. 矩阵快速幂——POJ3070

    矩阵快速幂和普通的快速幂差不多,只不过写起来比较麻烦一点,需要重载*运算符. 模板: struct mat { int m[maxn][maxn]; }unit; mat operator * (ma ...

  5. IOS-更优雅地使用Static Cell

    更优雅地使用Static Cell 在项目开发中,经常会用到static cell来实现一些固定的列表界面(如:个人中心等),在static cell被点击时,如何判断被点击的cell是哪一个,有什么 ...

  6. iOS多线程GCD详解

    在这之前,一直有个疑问就是:gcd的系统管理多线程的概念,如果你看到gcd管理多线程你肯定也有这样的疑问,就是:并发队列怎么回事,即是队列(先进先出)怎么会并发,本人郁闷了好久,才发现其实cgd管理多 ...

  7. idea如何热部署(转)

    IntelliJ IDEA 怎么热部署,每次修改java文件就得重启tomcat  原文链接:http://blog.csdn.net/dandandeshangni/article/details/ ...

  8. FIS 的思想和优点

    资源表 各种性能优化算法的加载框架 依赖声明有助于组件化 资源自动合并 链接 与webpack对比

  9. python实践报错:SyntaxError: Non-ASCII character

    报错: File "C:\001myWorkspace\001myWork\workspace2\MyFirstPython\src\demo4\demo4-2.py", line ...

  10. Oozie_03运行官方案例【20161116】

    3.1官方的案例 (1)Oozie根目录下找到 oozie-examples.tar.gz (2)解压tar -zxvf oozie-examples.tar.gz  生成example文件夹 [ha ...