gsoap入门实例
- 环境
VS2008,gsoap_2.8,win7 - 实例场景:在客户端输入一个字符串,然后传递给服务端计算字符串长度并返回给客户端(附加一些加减乘除法的实现);
- 将..\gsoap-2.8\gsoap\bin\win32中的两个exe文件所在路径加入环境变量中,后面有用;
- 新建一个文件夹,设计一个calculator.h文件,如下(前面几行的注释我也不知道有啥用)
//gsoap ns service name: add
//gsoap ns service namespace: http://localhost/add.wsdl
//gsoap ns service location: http://localhost
//gsoap ns service executable: add.cgi
//gsoap ns service encoding: encoded
//gsoap ns schema namespace: urn:add
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);
int ns__Hello(char* str,int *len); - 在该文件夹中打开cmd(方法ctrl+右键 --》在此处打开命令窗口),输入命令:soapcpp2.exe calculator.h
有关gsoap的命令用法自行百度; - 你会发现生成很多很多文件

- 利用VS2008创建一个calServer的工程,将calculator.h add.nsmap soapC.cpp soapServer.cpp soapH.h soapStub.h放入该工程,并且将gsoap-2.8\gsoap下的stdsoap2.h stdsoap2.cpp也添加到该工程;添加完后,在项目属性中的链接器--输入--附加依赖项 中输入wsock32.lib;
- 在calServer中添加一个main.cpp,代码如下:
#include "soapH.h"
#include "add.nsmap"
#include "stdio.h"
#include <iostream>
using namespace std; int main( int argc, char *argv[])
{
struct soap *CalculateSoap = soap_new(); //创建一个soap
int iSocket_master = soap_bind(CalculateSoap, NULL, , ); //绑定到相应的IP地址和端口()NULL指本机,
//10000为端口号,最后一个参数不重要。
if (iSocket_master< ) //绑定出错
{
soap_print_fault(CalculateSoap, stderr);
exit(-);
}
printf("SoapBind success,the master socket number is:%d\n",iSocket_master); //绑定成功返回监听套接字 while()
{
int iSocket_slaver = soap_accept(CalculateSoap);
if (iSocket_slaver < )
{
soap_print_fault(CalculateSoap, stderr);
exit(-);
}
printf("Get a new connection,the slaver socket number is:%d\n",iSocket_slaver); //绑定成功返回监听套接字
soap_serve(CalculateSoap);
soap_end(CalculateSoap);
}
soap_done(CalculateSoap);
free(CalculateSoap); return ;
} /*加法的具体实现*/
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 || == 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;
} int ns__Hello(struct soap *soap, char *str, int *len)
{
/*if (NULL == result)
{
printf("Error:The third argument should not be NULL!\n");
return SOAP_ERR;
}
else
{
cout << result <<endl;
return SOAP_OK;
}*/
//if (NULL == result)
cout << str <<endl;
(*len) = strlen(str);
return SOAP_OK; } - Server端完成,可以运行了。
- 客户端:利用VS2008创建一个calClient的工程,将calculator.h add.nsmap soapC.cpp soapClient.cpp soapH.h soapStub.h放入该工程,并且将gsoap-2.8\gsoap下的stdsoap2.h stdsoap2.cpp也添加到该工程;添加完后,在项目属性中的链接器--输入--附加依赖项 中输入wsock32.lib;
- 在calClient中添加一个main.cpp,代码如下:
#include "soapH.h"
#include "add.nsmap"
#include "stdio.h"
#include <iostream>
using namespace std; int main( int argc, char *argv[])
{
printf("The Client is runing...\n");
int num1 = ;
int num2 = ;
int result = ; struct soap *CalculateSoap = soap_new();
//soap_init(CalculateSoap);
注意改你的ip,端口10000不改
char * server_addr = "http://xx.x.x.x:10000"; 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);
}
char *str = new char[];
cin.getline(str,);
cout << str <<endl;
int len;
iRet = soap_call_ns__Hello(CalculateSoap,server_addr,"",str,&len);
if ( iRet == SOAP_ERR)
{
printf("Error while calling the soap_call_ns__add");
}
else
{
cout << str << " length is " << len <<" success!\n";
} soap_end(CalculateSoap);
soap_done(CalculateSoap);
free(CalculateSoap); return ;
} - Client端完成,可以先运行Server,在运行Client看看效果。
注意:客户端和服务端可以再两台电脑上允许并访问。
gsoap入门实例的更多相关文章
- React 入门实例教程(转载)
本人转载自: React 入门实例教程
- struts入门实例
入门实例 1 .下载struts-2.3.16.3-all .不摆了.看哈就会下载了. 2 . 解压 后 找到 apps 文件夹. 3. 打开后将 struts2-blank.war ...
- Vue.js2.0从入门到放弃---入门实例
最近,vue.js越来越火.在这样的大浪潮下,我也开始进入vue的学习行列中,在网上也搜了很多教程,按着教程来做,也总会出现这样那样的问题(坑啊,由于网上那些教程都是Vue.js 1.x版本的,现在用 ...
- wxPython中文教程入门实例
这篇文章主要为大家分享下python编程中有关wxPython的中文教程,分享一些wxPython入门实例,有需要的朋友参考下 wxPython中文教程入门实例 wx.Window 是一个基类 ...
- Omnet++ 4.0 入门实例教程
http://blog.sina.com.cn/s/blog_8a2bb17d01018npf.html 在网上找到的一个讲解omnet++的实例, 是4.0下面实现的. 我在4.2上试了试,可以用. ...
- Spring中IoC的入门实例
Spring中IoC的入门实例 Spring的模块化是很强的,各个功能模块都是独立的,我们可以选择的使用.这一章先从Spring的IoC开始.所谓IoC就是一个用XML来定义生成对象的模式,我们看看如 ...
- Node.js入门实例程序
在使用Node.js创建实际“Hello, World!”应用程序之前,让我们看看Node.js的应用程序的部分.Node.js应用程序由以下三个重要组成部分: 导入需要模块: 我们使用require ...
- Java AIO 入门实例(转)
Java7 AIO入门实例,首先是服务端实现: 服务端代码 SimpleServer: public class SimpleServer { public SimpleServer(int port ...
- Akka入门实例
Akka入门实例 Akka 是一个用 Scala 编写的库,用于简化编写容错的.高可伸缩性的 Java 和 Scala 的 Actor 模型应用. Actor模型并非什么新鲜事物,它由Carl Hew ...
随机推荐
- spark、storm与Hadoop
1. Storm是什么,怎么做,如何做的更好?Storm是一个开源的分布式实时计算系统,它可以简单.可靠地处理大量的数据流.Storm有很多应用场景,如实时分析.在线机器学习.持续计算.分布式RPC. ...
- JVM菜鸟进阶高手之路八(一些细节)
转载请注明原创出处,谢谢! gc日志问题 查看docker环境的gc日志,发现是下面这种情况,很奇怪,一直怀疑是docker环境那里是否有点问题,并没有怀疑配置,之前物理机上面的gc日志都是正常那种. ...
- StringBuffer和String的相互转换
1:用法: * A:String -- >StringBuffer * a:通过构造方法 * b:通过append()方法 * B:StringBuffer --> String * a: ...
- Redis学习——Redis持久化之AOF备份方式保存数据
新技术的出现一定是在老技术的基础之上,并且完善了老技术的某一些不足的地方,新技术和老技术就如同JAVA中的继承关系.子类(新技术)比父类(老技术)更加的强大! 在前面介绍了Redis学习--Redis ...
- 进入css3动画世界(二)
进入css3动画世界(二) 今天主要来讲transition和transform入门,以后会用这两种属性配合做一些动效. 注:本文面向前端css3动画入门人员,我对这个也了解不深,如本文写的有纰漏请指 ...
- Hive基础(1)---Hive是什么
1. Hive是什么 Hive是基于Hadoop的数据仓库解决方案.由于Hadoop本身在数据存储和计算方面有很好的可扩展性和高容错性,因此使用Hive构建的数据仓库也秉承了这些特性. 这是来自官方的 ...
- Ubuntu16.04.1安装JDK1.8.0
今天在安装Zookeeper的时候需要安装JDK,对于.Neter来说还是有点陌生,下面我就把安装JDK的步骤记录一下,分享给大家. 一.下载JDK安装包:http://www.oracle.com/ ...
- 【重点突破】——Canvas技术绘制随机改变的验证码
一.引言 本文主要是我在学习Canvas技术绘图时的一个小练习,绘制随机改变的验证码图片,虽然真正的项目里不这么做,但这个练习是一个掌握Canvas技术很好的综合练习.(真正的项目中验证码图片使用服务 ...
- eclipse建立一个jsp项目遇到的问题及解决
打开eclipse 在workplace 区域空白处,右键 填写好Project name,之后,点击finished 即可. 选中webcontent,新建一个文件夹,并新建一个jsp 文件 新建 ...
- 表空间tablespace,数据文件datafiles,和控制文件control files介绍
https://docs.oracle.com/cd/B19306_01/server.102/b14220/physical.htm#i5919 本文系翻译 表空间tablespace,数据文件da ...