版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、预备知识ONVIF规范中设备管理和控制部分所定义的接口均以Web Services的形式提供。ONVIF规范涵盖了完全的XML及WSDL的定义。每一个支持ONVIF规范的终端设备均须提供与功能相应的Web Service。服务端与客户端的数据交互采用SOAP协议。【来自ONVIF中的其他部分比如音视频流则通过RTP/RTSP进行 。那么WebServices、SOAP、WSDL、gSOAP又都是什么?假如我们需要开发一个linux上的app,这个app需要与远端的Web服务有一个交互,比如获取一个运算结果、或者是天气等,那么我们就需要使用WebServices。Web Services可以概述
2、为:Web Services 可以将应用程序转换为网络应用程序。通过使用 Web Services,应用程序可以向全世界发布信息,或提供某项功能。Web Services 可以被其他应用程序使用。通过 Web Services,会计部门的 Win 服务器可以与 IT 供应商的 UNIX 服务器相连接。基本的 Web Services 平台是 XML+HTTP。Web services 使用 XML 来编解码数据,并使用 SOAP 来传输数据。SOAP又是什么?SOAP 是基于 XML 的简易协议,可使应用程序在 HTTP 之上进行信息交换。或者更简单地说:SOAP 是用于访问网络服务的协议。对
3、于应用程序开发来说,使程序之间进行因特网通信是很重要的。目前的应用程序通过使用远程过程调用(RPC)在诸如 DCOM 与 CORBA 等对象之间进行通信,但是 HTTP 不是为此设计的。RPC 会产生兼容性以及安全问题;防火墙和代理服务器通常会阻止此类流量。通过 HTTP 在应用程序间通信是更好的方法,因为 HTTP 得到了所有的因特网浏览器及服务器的支持。SOAP 就是被创造出来完成这个任务的。SOAP 提供了一种标准的方法,使得运行在不同的操作系统并使用不同的技术和编程语言的应用程序可以互相进行通信。如何实现SOAP?我们要知道SOAP协议是基于XML的,那么如何能够将他们嵌入到C/C+的
4、应用程序里使用?gSOAP编译工具就提供了一个SOAP/XML 关于C/C+ 语言的实现,从而让C/C+语言开发web服务或客户端程序的工作变得轻松了很多。将与开发无关的SOAP协议的实现细节相关的内容对开发人员隐藏起来。因为SOAP提供的是一种标准化的方法,gSOAP的编译器能够自动的将用户定义的本地化的C或C+数据类型转变为符合XML语法的数据结构,这样,只用一组简单的API就将用户从SOAP细节实现工作中解脱了出来,可以专注与应用程序逻辑的实现工作了。并且可以跨越多个操作系统、语言环境以及在防火墙后的不同组织。更直白的说,使用gSOAP可以产生用于开发Web Services的SOAP通
5、信协议方面的代码框架,开发人员只需要实现server的被调用的函数,然后在client端就可以像调用本地函数一样调用在远端的函数。gSOAP包含两个工具wsdl2h和soapcpp2,用来产生代码框架。开发Web服务程序,需使用gSOAP生成服务器端和客户端代码框架(通常情况下之需要实现server端或者实现client,因为另一端通常是别人做好的,比如ipnc中的onvif,实现的server端)。我们有两种做法:编写WSDL,使用wsdl2h生成头文件,再soapcpp2生成框架代码;编写头文件,使用soapcpp2生成框架代码;这两种方式,结果是一样的,最终都有产生头文件,并生成代码。不
6、同在于,在项目的开发中需要维护的文件不同,前者是需要维护WSDL文件,后者维护头文件。SOAP调用示例下面就使用第二种方法来实现一个简单的通信实例:在远端实现两数相加,然后返回运算结果。1、下载gSOAPgSOAP-2.8软件包不需要安装,直接解压,在gsoap-2.8gsoapbin目录下是上面提到的两个命令行工具,包含win32、linux、maxOS等三种版本,在使用soapcpp2生产代码框架时一般需要gsoap-2.8gsoapimport目录下和gsoap-2.8gsoapcustom的 文件。在命令行中使用-I<PATH>包含进来即可。2、编写头文件:add.h
7、60;在这里我们不需要wsdl的文件,可以直接从.h文件来生成代码。我们定义一个函数声明文件,用来定义接口函数,名称为add.hcpp view plaincopyprint?1. /gsoapopt cw 2. /gsoap ns2 schema namespace: urn:add 3. /gsoap ns2 schema form: unqualified 4. /gsoap ns2 service na
8、me: add 5. /gsoap ns2 service type: addPortType 6. /gsoap ns2 service port:/engelen/addserver.cgi 7. /gsoap ns2 service namespace: urn:add 8. 9. /gsoap ns2&
9、#160; service method-style: add rpc 10. 11. /gsoap ns2 service method-action: add "" 12. int ns2_add( int num1, int num2,
10、;int* sum ); /gsoapopt cw/gsoap ns2 schema namespace: urn:add/gsoap ns2 schema form: unqualified/gsoap ns2 service name: add/gsoap ns2 service type: addPortType/gsoap ns2 service port:/engelen/addserver.cgi/gsoap ns2 service namespace: urn:add/gsoap ns2 se
11、rvice method-style: add rpc/gsoap ns2 service method-action: add ""int ns2_add( int num1, int num2, int* sum );3、产生代码框架 我们执行一下命令,自动生成一些远程调用需要的文件。(先将他们加如到系统环境变量中)soapcpp2 -c add.h-c是产生纯C代码,如果提示找不到typemap.dat,将gsoap-2.8gsoap下的typemap.dat复制到当前目录就可以了。通过上列命令我们会得到如下文件:先大概记住他们的名字,将来会提到他们。4、添加服务端代
12、码,创建文件:addserver.ccpp view plaincopyprint?1. #include "soapH.h" 2. #include "add.nsmap" 3. 4. int main(int argc, char *argv) 5. 6. int m, s; 7.
13、0; struct soap add_soap; 8. soap_init(&add_soap); 9. soap_set_namespaces(&add_soap, namespaces); 10. 11. if (argc < 2) &
14、#160; 12. printf("usage: %s <server_port> n", argv0); 13. exit(1); 14. else 15.
15、160; m = soap_bind(&add_soap, NULL, atoi(argv1), 100); 16. if (m < 0) 17. so
16、ap_print_fault(&add_soap, stderr); 18. exit(-1); 19. 20. fprintf(stderr, "Socke
17、t connection successful: master socket = %dn", m); 21. for (;) 22. s = soap_accept(&add_soap)
18、; 23. if (s < 0) 24. soap_print_fault(&add_soap, stderr); 25.
19、0; exit(-1); 26. 27. fprintf(stderr, &q
20、uot;Socket connection successful: slave socket = %dn", s); 28. soap_serve(&add_soap); 29.
21、soap_end(&add_soap); 30. 31. 32. return 0; 33. 34. #if 1 35. int ns2_add(struct soap *add_soap, int
22、160;num1, int num2, int *sum) 36. 37. *sum = num1 + num2; 38. return 0; 39. 40. #endif #include "soapH.h"#include "add.nsmap
23、"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", argv0); exit(1); else m = soap_bind(&add_soap, NULL, atoi(argv1), 100); if (m < 0
24、) soap_print_fault(&add_soap, stderr); exit(-1); fprintf(stderr, "Socket connection successful: master socket = %dn", m); for (;) s = soap_accept(&add_soap); if (s < 0) soap_print_fault(&add_soap, stderr); exit(-1); fprintf(stderr, "Socket connection successful: slave s
25、ocket = %dn", s); soap_serve(&add_soap); soap_end(&add_soap); return 0;#if 1int ns2_add(struct soap *add_soap, int num1, int num2, int *sum) *sum = num1 + num2; return 0;#endif5、添加客户端代码,创建文件:addclient.c cpp view plaincopyprint?1. #include "soapStub.h" 2. #inclu
26、de "add.nsmap" 3. 4. int add(const char *server, int num1, int num2, int *sum) 5. 6. struct soap add_soap; 7. int result
27、 = 0; 8. soap_init(&add_soap); 9. soap_set_namespaces(&add_soap, namespaces); 10. soap_call_ns2_add(&add_soap, server, NULL, num1, num2, sum);&
28、#160; 11. printf("server is %s, num1 is %d, num2 is %d/n", server, num1, num2); 12. 13. if (add_soap.error) 14. &
29、#160; printf("soap error: %d, %s, %sn", add_soap.error, *soap_faultcode(&add_soap), *soap_faultstring(&add_soap); 15. result = add_soap.error; 16.
30、60; 17. soap_end(&add_soap); 18. soap_done(&add_soap); 19. return result; 20. #include "soapStub.h"#include "add.nsmap"in
31、t add(const char *server, int num1, int num2, int *sum) struct soap add_soap; int result = 0; soap_init(&add_soap); soap_set_namespaces(&add_soap, namespaces); soap_call_ns2_add(&add_soap, server, NULL, num1, num2, sum); printf("server is %s, num1 is %d, num2 is %d/n", server,
32、num1, num2); if (add_soap.error) printf("soap error: %d, %s, %sn", 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;6、写客户端测试代码,创建文件:addtest.c cpp view plaincopyprint?1
33、. #include <stdio.h> 2. #include <stdlib.h> 3. #include <string.h> 4. 5. int add(const char *server, int num1, int num2, int *sum); 6. int main(int argc,
34、60;char *argv) 7. 8. int result = -1; 9. char server128 = 0; 10. int num1; 11. int num2; 12.
35、 int sum; 13. 14. if (argc < 4) 15. printf("usage: %s <ip:port> num1 num2 n", argv0);
36、0;16. exit(1); 17. 18. 19. strcpy(server,argv1); 20. num1 = atoi(argv2); 21. num2 =&
37、#160;atoi(argv3); 22. result = add(server, num1, num2,&sum); 23. 24. if (result != 0) 25. printf("soap e
38、rror, errcode=%dn", result); 26. else 27. printf("%d + %d = %dn", num1, num2, sum); 28.
39、;29. return 0; 30. #include <stdio.h>#include <stdlib.h>#include <string.h>int add(const char *server, int num1, int num2, int *sum);int main(int argc, char *argv) int result = -1; char server128 = 0; int num1; int num2; int sum;
40、if (argc < 4) printf("usage: %s <ip:port> num1 num2 n", argv0); exit(1); strcpy(server,argv1); num1 = atoi(argv2); num2 = atoi(argv3); result = add(server, num1, num2,&sum); if (result != 0) printf("soap error, errcode=%dn", result); else printf("%d + %d = %dn&q
41、uot;, num1, num2, sum); return 0;7、编写Makefile,编译前,先将gsoap-2.8gsoap目录下的stdsoap2.c和stdsoap2.h复制到当前目录下,它提供了对SOAP协议的简单调用。 cpp view plaincopyprint?1. GSOAP_ROOT = /root/onvif/gsoap-2.8/gsoap 2. CC = gcc -g -DWITH_NONAMESPACES 3. INCLUDE = -I$(
42、GSOAP_ROOT) 4. SERVER_OBJS = soapC.o stdsoap2.o soapServer.o addserver.o 5. CLIENT_OBJS = soapC.o stdsoap2.o soapClient.o addclient.o addtest.o 6. 7. all: server 8. server:
43、$(SERVER_OBJS) 9. $(CC) $(INCLUDE) -o addserver $(SERVER_OBJS) 10. 11. client: $(CLIENT_OBJS) 12. $(CC) $(INCLUDE) -o addtest $(CLIENT_OBJS) 13. 14. clean: 15. rm -f *.o addtest
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 湿气测试方法结果解读手册
- 在线安全教育培训管理制度
- 四季养生膳食调理操作规范
- 烤烟移栽后田间管理操作规程
- 糖尿病一日三餐配餐服务指南
- 草莓脱毒苗繁育操作技术规范
- 中医足疗技师操作规范
- 胃病康复期饮食禁忌指南标准
- 广东省珠海市2026年第二学期九年级第一次模拟考试数学试卷附答案
- 厂界噪声监测控制规范流程
- 西藏自治区日喀则市2026届高三第二次模拟考试语文试卷含解析
- 辽宁省能源集团招聘笔试题库2026
- 管道拆除安全措施方案
- 成人2型糖尿病口服降糖药联合治疗专家共识(2025版)课件
- 英语北京市昌平区2026年高三年级第一次统一练习(昌平高三一模)(4.7-4.10)
- 2026成都市八年级语文下册部编版期末考试卷含答案
- 便利店工作制度详细流程
- 2025秋季《中华民族共同体概论》期末综合考试-国开(XJ)-参考资料
- 高速公路服务区建设项目可行性研究报告
- LY/T 2015-2012大熊猫饲养管理技术规程
- 文史资料选辑合订本(46卷本第1辑至第136辑)
评论
0/150
提交评论