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. HTML 4.01+5基礎知識

    HTML 4.01+5 1.Html結構:html>head+body 2.Html快捷鍵:!加Tab(在sublime中) 3.雙標籤: ①常用標籤 h1.h2.h3.h4.h5.h6 p.c ...

  2. AngularJS 框架

    AngularJS 通过 指令 扩展了 HTML,且通过 表达式 绑定数据到 HTML. [Angular JS表达式]  1.Angular JS使用双{{}}绑定方式.用于将表达式的内容输出到页面 ...

  3. memcached readme

    memcache======== http://www.cnblogs.com/jeffwongishandsome/archive/2011/11/06/2238265.html # 命令 ## 存 ...

  4. 在JavaScript中使用json.js:访问JSON编码的某个值

    演示: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...

  5. POJ 3923 Ugly Windows(——考察思维缜密性的模拟题)

    题目链接: http://poj.org/problem?id=3923 题意描述: 输入一个n*m的屏幕 该屏幕内有至少一个对话框(每个对话框都有对应的字母表示) 判断并输出该屏幕内处于最表层的对话 ...

  6. spring boot系列01--快速构建spring boot项目

    最近的项目用spring boot 框架 借此学习了一下 这里做一下总结记录 非常便利的一个框架 它的优缺点我就不在这背书了 想了解的可以自行度娘谷歌 说一下要写什么吧 其实还真不是很清楚,只是想记录 ...

  7. hdu4704 Sum 2013 Multi-University Training Contest 10 数论题

    Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Subm ...

  8. vue环境搭建

    1.Window 上安装Node.js 1.Windows 安装包(.msi) 32 位安装包下载地址 : https://nodejs.org/dist/v4.4.3/node-v4.4.3-x86 ...

  9. Win CE 6.0 获取手持机GPS定位2----示例代码 (C#)

    一.须知 1.手持机(PDA)必须有GPS模块,才能通过代码使用串口通信获取GPS相关信息 2.要清楚自己手持机(PDA)固定的GPS通信串口号,如我们公司的手持机获取GPS信息的串口为COM4 3. ...

  10. Java继承--覆盖

    java中支持单继承.不直接支持多继承,但对C++中的多继承机制进行改良. 单继承:一个子类只能有一个直接父类. 多继承:一个子类可以有多个直接父类(java中不允许,进行改良).不直接支持,因为多个 ...