---恢复内容开始---html
以前曾经编写过简单的样例,好久没有碰过,发现已经所有忘记,现在又须要从新巩固一下。c++
首先是下载gsoap,没法访问官方下载页面,只能在网上搜索,找到一个2.8版本存入云盘以防再次找不到。web
下面记录一下,经过gsoap建立web Service的过程。网络
1.建立一个项目文件夹calcapp
2.在calc文件夹中,建立一个头文件calc.cppsocket
// 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程序是为操做方便,以前添加进去的。spa
注:关于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项目中并无使用这两个文件。
参考文章:
2)gSOAP Calculator Service and Client
3)The gSOAP Toolkit for SOAP and REST Web Services and XML-Based Applications