gSOAP:C++编写服务器端
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++编写服务器端的更多相关文章
- 使用Qt编写服务器端程序(包括Http传输服务器端)的方法
使用Qt编写客户端的程序的示例或demo较多,但是编写服务器端程序的demo很少.当然,服务器端的程序一般不需要带界面,这点我们可以理解.不过有些时候我们还是需要使用Qt编写一个简单的测试用的服务器代 ...
- [转贴]gsoap使用心得!
最近换了个工作环境,现在在大望路这边上班,呵,刚上班接到的任务就是熟悉gsoap!废话少说,现在开始gSoap学习! gSOAP是一个夸平台的,用于开发Web Service服务端和客户端的工具,在W ...
- vc++2008 采用GSoap访问 WebService
(转http://www.cppblog.com/yeqing/articles/12762.html) 前一阶段写gSOAP 的文章没保存好,后来想写的,越学越没有写的勇气了,感觉自己很菜,但是现在 ...
- nodejs 编写(添加时间戳)命令行工具 timestamp
Nodejs除了编写服务器端程序还可以编写命令行工具,如gulp.js就是Nodejs编写的. 接下来我们来实现一个添加时间戳的命令: $ timestamp action https://www.n ...
- Socket编程——客户端,服务器端的读写操作
URL网络编程,最大的特征就是一对一的响应! 1:客户端“写”,服务器端用于“读” package coreBookSocket2; import java.io.InputStreamReader; ...
- Android与服务器端数据交互(http协议整合struts2+android)
在android中有时候我们不需要用到本机的SQLite数据库提供数据,更多的时候是从网络上获取数据,那么Android怎么从服务器端获取数据呢?有很多种,归纳起来有 一:基于Http协议获取数据方法 ...
- 入职第一天:前端leader手把手教我入门Vue服务器端渲染(SSR)
继前段时间西安电面之后顺利拿到了OFFER,今天(5月2号)是我入职第一天,在简短的内部培训了一上午后,前端leader让我先了解下什么是vue的服务器端渲染(SSR). SSR,英文全称叫 Serv ...
- 服务器端网络编程之 IO 模型
引言 从 T 跳槽到 A 之后,我的编程语言也从 C++ 转为 了 Java.在 T 做的偏服务器端开发,而在 A 更偏向于业务开发.上周在 A 公司组内做了一个<服务器端高性能网络编程> ...
- gSOAP 初体验
安装 由于本人使用的是 Mac OS 系统,故以 Mac OS 为例说明如何安装 gSOAP. 1)下载 gSOAP 可以在 https://sourceforge.net/projects/gsoa ...
随机推荐
- python 浮点数转分数
from fractions import Fraction value = 4.2 print(Fraction(value).limit_denominator())
- Facade(外观)
意图: 为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 适用性: 当你要为一个复杂子系统提供一个简单接口时.子系统往往因为不断演化而变 ...
- RxJava和RxAndroid
现在RxJava和RxAndroid越来越火爆,自己在业余时间也学习了一下,感觉确实很好用,之前 为了完成页面刷新,数据请求,组件信息传递的时候,要使用handler,真的是逻辑思路很强,稍微不注意, ...
- [转] oracle 监听
oracle 监听 启动监听:lsnrctl start 查看监听:lsnrctl status 停止监听:lsnrctl stop 1.oracle 数据服务器包括:实例进程和数据库: 实例进程包括 ...
- 向多页TABLE中插入数据时,新增行总是在当前页的最后一行
CODE IN CO OATableBean table = (OATableBean)webBean.findChildRecursive("LineTable"); int n ...
- 【Matplotlib】线设置,坐标显示范围
改变线的颜色和线宽 参考文章: controlling line properties Line API 线有很多属性你可以设置:线宽,线型,抗锯齿等等:具体请参考matplotlib.lines.L ...
- Java——三大特性
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- Qt Creatror使用designer修改了界面但是编译无反应的解决方法
这个问题主要是UI没有更新导致的,根治的方法为: 项目中的.pro内增加 UI_DIR=./UI,同时删除掉源代码目录中ui_*.h,clear all,->qmake->rebuilt ...
- python的单元测试代码编写流程
单元测试: 单元测试是对单独的代码块分别进行测试, 以确保它们的正确性, 单元测试主要还是由开发人员来做, 其余的集成测试和系统测试由专业的测试人员来做. python的单元测试代码编写主要记住以下几 ...
- 内存泄漏 之 MAT工具的使用
1 内存泄漏的排查方法 Dalvik Debug Monitor Server (DDMS) 是 ADT插件的一部分,其中有两项功能可用于内存检查 : · heap 查看堆的分配情况 · ...