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 ...
随机推荐
- IE8 JSON is not defined
问题原因: 昨天遇到了一个问题.就是用ajax从后台查询数据时,返回信息无法显示,经过提示发现是IE控制台提示: JSON is not defined 错误. 而且这个问题在本人自己的电脑上是不存在 ...
- 在触发器中,当“IsMouseOver”属性=true时,设置当前控件的高亮选中效果
<Style.Triggers> <!--<Trigger Property="IsSelected" Value="Tru ...
- [oracle]创建查看 LOCAL INDEX
create index IDX_T_GPS_CPH_local on T_GPS (CPH) local; create index IDX_T_GPS_SJ_local on T_GPS (SJ) ...
- IOS-网络(ASIHTTPRequest的使用简介)
使用iOS SDK中的HTTP网络请求API,相当的复杂,调用很繁琐,ASIHTTPRequest就是一个对CFNetwork API进行了封装,并且使用起来非常简单的一套API,用Objective ...
- 011PHP文件处理——文件处理 文件内容分页操作类
<?php /** * 文件内容分页操作类: */ //访问地址:http://basicphp.com/006file/011.php?&page=1 class StrPage { ...
- 99%的人都理解错了HTTP中GET与POST的区别(转自知乎)
作者:Larry链接:https://zhuanlan.zhihu.com/p/22536382来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. GET和POST是HTT ...
- tcpdump学习笔记
简介 简单的说,tcpdump就是一个抓包工具,类似Wireshark. tcpdump可以根据使用者的定义过滤/截取网络上的数据包,并进行分析.tcpdump可以将数据包的头部完全接 ...
- myeclipse web servelet调试输入的中文在TOMCAT服务器的命令行显示为????
B 问题:myeclipse web servelet调试输入的中文在TOMCAT服务器的命令行显示为???? 解决:调整JSP页面编码:gb2312--->utf-8
- 用 Unity 和 HTC Vive 实现高级 VR 机制(1)
原文:Advanced VR Mechanics With Unity and the HTC Vive Part 1 作者:Eric Van de Kerckhove 译者:kmyhy VR 从来没 ...
- np.ones(N)/N的作用
在python中导入numpy包 N=5 weights = np.ones(N)/N //这里就相当于创建了一个数组,且为5个1/5的数组 print "weights&quo ...