1. 环境
    VS2008,gsoap_2.8,win7
  2. 实例场景:在客户端输入一个字符串,然后传递给服务端计算字符串长度并返回给客户端(附加一些加减乘除法的实现);
  3. 将..\gsoap-2.8\gsoap\bin\win32中的两个exe文件所在路径加入环境变量中,后面有用;
  4. 新建一个文件夹,设计一个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);
  5. 在该文件夹中打开cmd(方法ctrl+右键 --》在此处打开命令窗口),输入命令:soapcpp2.exe calculator.h
    有关gsoap的命令用法自行百度;
  6. 你会发现生成很多很多文件
  7. 利用VS2008创建一个calServer的工程,将calculator.h  add.nsmap soapC.cpp soapServer.cpp soapH.h soapStub.h放入该工程,并且将gsoap-2.8\gsoap下的stdsoap2.h stdsoap2.cpp也添加到该工程;添加完后,在项目属性中的链接器--输入--附加依赖项 中输入wsock32.lib;
  8. 在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; }
  9. Server端完成,可以运行了。
  10. 客户端:利用VS2008创建一个calClient的工程,将calculator.h  add.nsmap soapC.cpp soapClient.cpp soapH.h soapStub.h放入该工程,并且将gsoap-2.8\gsoap下的stdsoap2.h stdsoap2.cpp也添加到该工程;添加完后,在项目属性中的链接器--输入--附加依赖项 中输入wsock32.lib;
  11. 在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 ;
    }
  12. Client端完成,可以先运行Server,在运行Client看看效果。
    注意:客户端和服务端可以再两台电脑上允许并访问。

gsoap入门实例的更多相关文章

  1. React 入门实例教程(转载)

    本人转载自: React 入门实例教程

  2. struts入门实例

    入门实例 1  .下载struts-2.3.16.3-all  .不摆了.看哈就会下载了. 2  . 解压  后 找到 apps 文件夹. 3.    打开后将 struts2-blank.war   ...

  3. Vue.js2.0从入门到放弃---入门实例

    最近,vue.js越来越火.在这样的大浪潮下,我也开始进入vue的学习行列中,在网上也搜了很多教程,按着教程来做,也总会出现这样那样的问题(坑啊,由于网上那些教程都是Vue.js 1.x版本的,现在用 ...

  4. wxPython中文教程入门实例

    这篇文章主要为大家分享下python编程中有关wxPython的中文教程,分享一些wxPython入门实例,有需要的朋友参考下     wxPython中文教程入门实例 wx.Window 是一个基类 ...

  5. Omnet++ 4.0 入门实例教程

    http://blog.sina.com.cn/s/blog_8a2bb17d01018npf.html 在网上找到的一个讲解omnet++的实例, 是4.0下面实现的. 我在4.2上试了试,可以用. ...

  6. Spring中IoC的入门实例

    Spring中IoC的入门实例 Spring的模块化是很强的,各个功能模块都是独立的,我们可以选择的使用.这一章先从Spring的IoC开始.所谓IoC就是一个用XML来定义生成对象的模式,我们看看如 ...

  7. Node.js入门实例程序

    在使用Node.js创建实际“Hello, World!”应用程序之前,让我们看看Node.js的应用程序的部分.Node.js应用程序由以下三个重要组成部分: 导入需要模块: 我们使用require ...

  8. Java AIO 入门实例(转)

    Java7 AIO入门实例,首先是服务端实现: 服务端代码 SimpleServer: public class SimpleServer { public SimpleServer(int port ...

  9. Akka入门实例

    Akka入门实例 Akka 是一个用 Scala 编写的库,用于简化编写容错的.高可伸缩性的 Java 和 Scala 的 Actor 模型应用. Actor模型并非什么新鲜事物,它由Carl Hew ...

随机推荐

  1. RocketMQ之双Master方式部署以及简单使用

    1.1.服务器环境 192.168.100.24 root nameServer1,brokerServer1 Master1 192.168.100.25 root nameServer2,brok ...

  2. Excel开发之旅(二)----数据的读写

    1.要实现数据的读写,首先,我们需要添加引用: using Excel=Microsoft.Office.Interop.Excel; 直接在项目中添加即可. 2.给3个按钮添加响应事件,工程代码截图 ...

  3. JAVA设计模式总结之六大设计原则

    从今年的七月份开始学习设计模式到9月底,设计模式全部学完了,在学习期间,总共过了两篇:第一篇看完设计模式后,感觉只是脑子里面有印象但无法言语.于是决定在看一篇,到9月份第二篇设计模式总于看完了,这一篇 ...

  4. hdu3018欧拉回路题

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  5. hdu1512 Monkey King(左偏树 + 并查集)

    Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things in its o ...

  6. 【机器学习实战】第11章 使用 Apriori 算法进行关联分析

    第 11 章 使用 Apriori 算法进行关联分析 关联分析 关联分析是一种在大规模数据集中寻找有趣关系的任务. 这些关系可以有两种形式: 频繁项集(frequent item sets): 经常出 ...

  7. rem绝对自适应方案

    rem css3新增的rem是现在非常受欢迎的单位.看一下MDN上的说明: 这个单位代表根元素的 font-size 大小(例如 <html> 元素的font-size). 使用这个单位可 ...

  8. C-多个行内块布局

    1 消除间隔

  9. sublime text注册码(秘钥)

    —– BEGIN LICENSE —– TwitterInc 200 User License EA7E-890007 1D77F72E 390CDD93 4DCBA022 FAF60790 61AA ...

  10. jsp web JavaBean MVC 架构 EL表达式 EL函数 JSTL

     一.JavaBean概念(非常重要) 1.JavaBean就是遵循一定书写规范的Java类型(开发中:封装数据) a.必须有默认的构造方法,类必须是public的   public class  ...