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 ...
随机推荐
- Finally-操作返回值
Finally中操作返回会出现一个问题?直接看代码 static int M1() { ; try { result = result + ; //======引发异常的代码========== , ...
- jmeter测试教程
http://www.cnblogs.com/TankXiao/p/4045439.html
- 使用千位分隔符(逗号)表示web网页中的大数字
做手机端页面我们常常遇到数字,而在Safari浏览器下这些数字会默认显示电话号码,于是我们就用到了补坑的方法加入<meta>标签: <meta name="format-d ...
- E. Fish (概率DP)
E. Fish time limit per test 3 seconds memory limit per test 128 megabytes input standard input outpu ...
- 集 降噪 美颜 虚化 增强 为一体的极速图像润色算法 附Demo程序
在2015年8月份的时候,决心学习图像算法. 几乎把当时市面上的图像算法相关书籍都看了一遍, 资金有限,采取淘宝买二手书,长期驻留深圳图书馆的做法, 进度总是很慢,学习算法不得其法. 虽然把手上所有书 ...
- Elixir游戏服设计六
接上章,我新建了个app做包含Table模型, TableServer等.Table桌子的代码暂时如下, 有一些状态还没用上 defmodule Table do @state_accept 0 #准 ...
- webpack2使用ch9-处理模板文件 .html .ejs .tpl模板使用
1 目录展示 安装依赖 "ejs-loader": "^0.3.0","html-loader": "^0.4.5", ...
- winwebmail设置能用foxmail收发邮件
域名解析注意 1.首先做A记录解析: 主机名处:输入 mail IP地址处:输入IP地址 2.做MX记录: 主机名处: 大都保持空输入,什么也不用输入 TTL:默认就可以了,不需要改动 优先级:一 ...
- c#(asp.net) 多线程示例,用于同时处理多个任务
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- Hadoop2.0 HA集群搭建步骤
上一次搭建的Hadoop是一个伪分布式的,这次我们做一个用于个人的Hadoop集群(希望对大家搭建集群有所帮助): 集群节点分配: Park01 Zookeeper NameNode (active) ...