win7 gsoap与vs2010 c++创建Web Service

---恢复内容开始---

之前曾经编写过简单的样例,很久没有碰过,发现已经全部忘记,如今又需要重新巩固一下。

首先是下载gsoap,无法访问官方下载页面,只能在网上搜索,找到一个2.8版本存入云盘以防再次找不到。

下面记录一下,通过gsoap创建web Service的过程。

1.创建一个项目文件夹calc

2.在calc文件夹中,创建一个头文件calc.cpp

// Contents of file "calc.h": 
//gsoap ns service name: calculator
//gsoap ns service style: rpc
//gsoap ns service encoding: encoded
//gsoap ns service port: http://localhost:8080/calc/calculator.cgi
//gsoap ns service namespace: urn:calculator
int ns__add(double a, double b, double &result); 
int ns__sub(double a, double b, double &result); 
int ns__sqrt(double a, double &result); 

需要注意的是注释部分不可省略,该部分记录了一些重要的配置信息。

3.下载gsoap压缩包,获取/gsoap-2.8/gsoap/中的stdsoap.cpp与stdsoap.h文件(注意:本文使用c++因此是stdsoap.cpp文件,若使用c应该选取stdsoap.c文件。该文件提供web service项目中所需函数的声明与实现)。获取/gsoap-2.8/gsoap/bin/win32/中的soapcpp2.exe(用来解析创建的头文件),并将其放入calc.h所在的calc文件夹中(方便执行命令行,当然也可不用放在此路径下cmd命令复杂些:soapcpp2 (calc.h的路径)/calc.h -I (指定生成文件目录))。

4.启动cmd窗体,切换至soapcpp2.exe所在目录,执行 soapcpp2 calc.h 生成如下文件:

其中的calc.cpp是后期自己添加的文件,soappcpp2程序是为操作方便,之前添加进去的。

注:关于soapcpp2的命令的使用可以通过soapcpp2 -h来查看。

4.开始创建calculator(Win32 控制台应用程序) 解决方案(当前默认添加的项目为calculator,本文将其定义为server端,稍后会再添加项目定义为Client端)。

为了简便,在此创建一个空项目。

5.进行网络通信,添加WSock32.lib。

结果如下图:

点击应用、确定即添加完毕。

6.打开此项目目录,添加文件如下:

其中calc.cpp内容如下(calc.h方法实现):

 

#include "soapH.h"
#include "calculator.nsmap"
#include <math.h> 

int main(int argc, char **argv)
{
    int m, s;
    struct soap add_soap;
    soap_init(&add_soap);
    soap_set_namespaces(&add_soap,namespaces);
    if(argc < 2){
        printf("usage: %s <server_port> \n",argv[0]);
        exit(1);
    }else{
        m = soap_bind(&add_soap,NULL,atoi(argv[1]),100);
        if(m < 0){
            soap_print_fault(&add_soap,stderr);
            exit(-1);
        }
        fprintf(stderr, "Socket connection successful: master socket = %d\n", m);
        for(;;){
            s = soap_accept(&add_soap);
            if(s < 0){
                soap_print_fault(&add_soap,stderr);
                exit(-1);
            }
            fprintf(stderr, "Socket connection successful: slave socket = %d\n", s);  
            soap_serve(&add_soap);  
            soap_end(&add_soap); 
        }
    }
    //soap_serve(soap_new()); 
    return 0;
}

// Implementation of the "add" remote method: 
int ns__add(struct soap *soap, double a, double b, double &result) 
{ 
   result = a + b; 
   return SOAP_OK; 
} 
// Implementation of the "sub" remote method: 
int ns__sub(struct soap *soap, double a, double b, double &result) 
{ 
   result = a - b; 
   return SOAP_OK; 
} 
// Implementation of the "sqrt" remote method: 
int ns__sqrt(struct soap *soap, double a, double &result) 
{ 
   if (a >= 0) 
   { 
      result = sqrt(a); 
      return SOAP_OK; 
   } 
   else
   { 
      return soap_sender_fault(soap, "Square root of negative value", "I can only compute the square root of a non-negative value");
   } 
} 

 

 

7.向项目中添加文件:

其中calc.cpp为新建项,其余为现有项,最终结果如图:

8.生成项目,报错如下;

搜索解决方法,如下:

9.右键项目,重新生成即可。

10.右键解决方案,新建calcClient项目,同样为空项目,步骤如创建Server端相同,也需要添加WS32.lib,在添加所需文件时不同,文件如下:

11.calcClient.cpp如下:

#include "soapStub.h"  
#include "calculator.nsmap"  

int add(const char *server, int num1, int num2, double &sum)
{  
    struct soap add_soap;  
    int result = 0;  
    soap_init(&add_soap);  
    soap_set_namespaces(&add_soap, namespaces);  
    soap_call_ns__add(&add_soap, server, NULL, num1, num2, sum);  
    printf("server is %s, num1 is %d, num2 is %d\n", server, num1, num2);  
  
    if (add_soap.error) {  
        printf("soap error: %d, %s, %s\n", add_soap.error, *soap_faultcode(&add_soap), *soap_faultstring(&add_soap));  
        result = add_soap.error;  
    }
    soap_end(&add_soap);  
    soap_done(&add_soap);  
    return result;  
} 

calcClientTest.cpp如下:

    #include <stdio.h>  
    #include <stdlib.h>  
    #include <string.h>  
      
    int add(const char *server, int num1, int num2, double &sum);
    int main(int argc, char **argv)  
    {  
        int result = -1;  
        char server[128] = {0};  
        int num1;  
        int num2;  
        double sum;  
      
        if (argc < 4) {  
            printf("usage: %s <ip:port> num1 num2 \n", argv[0]);  
            exit(1);  
        }  
      
        strcpy(server,argv[1]);  
        num1 = atoi(argv[2]);  
        num2 = atoi(argv[3]);  
        result = add(server, num1, num2, sum);  
      
        if (result != 0) {  
            printf("soap error, errcode=%d \n", result);  
        } else {  
            printf("%d + %d = %f \n", num1, num2, sum);  
        }  
        return 0;  
    }  

最终项目结构如下:

12.修改清单工具中的输入输出,将嵌入清单改为“否”,否则会报如前的链接错误。

13.生成Client端。

14.运行Server与Client。

启动cmd切换至calculator.exe所在目录,执行:calculator 8080 启动Server端

启动cmd切换至calcClient.exe所在目录,执行:calcClient localhost:8080 7 7请求Server端

小结:

现在还是soap与c++的初学者,很多还不了解,走通这个程序,谨记一下。主要难点如下:

1)创建Server与Client端时,不知该添加哪些文件(应该先查文献,看看生成的文件都起什么作用)。

2)添加WSock32.lib时,不知如何添加(查询如何在vs项目中添加lib)。

3)生成项目时,出现链接错误(网上说修改清单工具的方法好像不太好,以后要好好看一下)。

4)对于gsoap的配置信息不了解,需要好好补补。

疑点:

1)在calc.h中配置的//gsoap ns service port: http://localhost:8080是否有用,怎么在启动Server端时,需要绑定端口号?

2)生成的wsdl文件和ns文件是否有用,在Server与Client项目中并没有使用这两个文件。

参考文章:

1)onvif学习2-soap介绍以及gsoap使用

2)gSOAP Calculator Service and Client

3)The gSOAP Toolkit for SOAP and REST Web Services and XML-Based Applications

 

posted @ 2015-01-28 18:16  idealing  阅读(1123)  评论(0编辑  收藏  举报