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 ...
随机推荐
- mybatis入门篇基——基本配置与参数说明
Mybatis 好吧这是我第一次写这种文章~如果有不足和错误之处欢迎评论,指点.今天想谈谈关于mybatis的一些基础入门知识. 进入正题~~: a.关于mybatis: 我个人觉得mybatis深得 ...
- KMP算法的来龙去脉
1. 引言 字符串匹配是极为常见的一种模式匹配.简单地说,就是判断主串TT中是否出现该模式串PP,即PP为TT的子串.特别地,定义主串为T[0-n−1]T[0-n−1],模式串为P[0-p−1]P[0 ...
- web应用中的异常处理
楼主前几天写了一篇“Java子线程中的异常处理(通用)”文章,介绍了在多线程环境下3种通用的异常处理方法. 但是平时大家的工作一般是基于开发框架进行的(比如Spring MVC,或Spring Boo ...
- php基础运算符语句
/* 多行注释 *///常用数据类型//int string double/float bool//变量的定义$a = 123;$b = "123";$c = '456';//$d ...
- structs2的核心和工作原理
在学习struts2之前,首先我们要明白使用struts2的目的是什么?它能给我们带来什么样的好处? 设计目标 Struts设计的第一目标就是使MVC模式应用于web程序设计.在这儿MVC模式的 ...
- 【MVC】MvcPager分页及边界传递数据示例
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- SQL Server XML数据解析
--5.读取XML --下面为多种方法从XML中读取EMAIL DECLARE @x XML SELECT @x = ' <People> <dongsheng> <In ...
- 传统 HTML 表单数据的“整存整取”
在日常开发中,涉及表单的处理司空见惯.过往,在取值和赋值的过程中,借助 jQuery 常常只是逐个控件进行操作,可惜这样开发效率并不高.那么能不能批量获取整个表单的值呢,以及批量为表单赋值. 一.取值 ...
- java连接sqlserver2008r2 心得
现在是该轻松一笑的时候了,困扰已久的问题有了解释了. 之前的各种连不上,说到底还是权限问题,sqlserver2008r2的权限分得太细了. 两个实例间数据库互相都看不到,更不用谈访问了. 端口号也是 ...
- 运维&网络知识(一)
1. DNS 域名系统(Domain Name System),因特网上作为域名和IP地址映射的一个分布式数据库.