gsoap使用
一. 安装gsoap
下载地址:http://sourceforge.net/projects/gsoap2/files/
解压安装:./configure --prefix=/usr/local/gsoap && make && make install
示例目录:gsoap-2.8/gsoap/samples 有各类语言使用接口方法
async chaining gmt Makefile mashup README.txt udp xml-rpc-json
atom chaining++ googleapi Makefile.am mashup++ rest varparam
autotest curl hello Makefile.cpp_proxy_rules mtom roll wcf
aws databinding httpcookies Makefile.cpp_proxy_rules_j mtom-stream router webserver
calc dime link Makefile.cpp_rules oneway rss wsa
calc++ dom link++ Makefile.c_rules oneway++ ssl wsrm
calc_vs2005 factory lu Makefile.defines polytest template wsse
calc_xcode factorytest magic Makefile.in primes testmsgr wst
二. 编写demo
定义函数接口,写chw.h文件
//gsoap ns service name: chw Simple calculator service
//gsoap ns service style: rpc
//gsoap ns service encoding: encoded
//gsoap ns service namespace:
//gsoap ns service location: //gsoap ns schema namespace: urn:chw //gsoap ns service method-documentation: add Sums two values
int add(int a, int b, int *result); //gsoap ns service method-documentation: sub Subtracts two values
int sub(int a, int b, int *result);
使用命令:soapcpp2 -c -x chw.h
-c 产生纯C代码,否则是C++代码(与头文件有关)
-I 指定import路径
-x 不要产生XML示例文件
生成文件有:
[root@Logcen5 test]# ls
chw.h soapClient.c soapH.h soapServer.c soapStub.h
soapC.c soapClientLib.c soap.nsmap soapServerLib.c
查看soapStub.h文件
/******************************************************************************\
* *
* Client-Side Call Stub Functions *
* *
\******************************************************************************/ /** Web service synchronous operation 'soap_call_add' to the specified endpoint and SOAP Action header, returns SOAP_OK or error code */
SOAP_FMAC5 int SOAP_FMAC6 soap_call_add(struct soap *soap, const char *soap_endpoint, const char *soap_action, int a, int b, int *result);
/** Web service asynchronous operation 'soap_send_add' to send a request message to the specified endpoint and SOAP Action header, returns SOAP_OK or error code */
SOAP_FMAC5 int SOAP_FMAC6 soap_send_add(struct soap *soap, const char *soap_endpoint, const char *soap_action, int a, int b);
/** Web service asynchronous operation 'soap_recv_add' to receive a response message from the connected endpoint, returns SOAP_OK or error code */
SOAP_FMAC5 int SOAP_FMAC6 soap_recv_add(struct soap *soap, int *result); /** Web service synchronous operation 'soap_call_sub' to the specified endpoint and SOAP Action header, returns SOAP_OK or error code */
SOAP_FMAC5 int SOAP_FMAC6 soap_call_sub(struct soap *soap, const char *soap_endpoint, const char *soap_action, int a, int b, int *result);
/** Web service asynchronous operation 'soap_send_sub' to send a request message to the specified endpoint and SOAP Action header, returns SOAP_OK or error code */
SOAP_FMAC5 int SOAP_FMAC6 soap_send_sub(struct soap *soap, const char *soap_endpoint, const char *soap_action, int a, int b);
/** Web service asynchronous operation 'soap_recv_sub' to receive a response message from the connected endpoint, returns SOAP_OK or error code */
SOAP_FMAC5 int SOAP_FMAC6 soap_recv_sub(struct soap *soap, int *result); /******************************************************************************\
* *
* Server-Side Operations *
* *
\******************************************************************************/ /** Web service operation 'add' implementation, should return SOAP_OK or error code */
SOAP_FMAC5 int SOAP_FMAC6 add(struct soap*, int a, int b, int *result);
/** Web service operation 'sub' implementation, should return SOAP_OK or error code */
SOAP_FMAC5 int SOAP_FMAC6 sub(struct soap*, int a, int b, int *result);
另外需要从gsoap拷贝两个文件过来stdsoap2.c和stdsoap2.h,方便gcc编译不报错!
编写自己的服务端。server.c
#include <stdio.h>
#include "soapH.h"
#include "soap.nsmap" int main(int argc, char *argv[])
{
struct soap soap;
SOAP_SOCKET m, s;
int ret;
soap_init(&soap);
if (argc < 2)
soap_serve(&soap);
else
{
m = soap_bind(&soap, NULL, atoi(argv[1]), 100);
if (!soap_valid_socket(m))
{
soap_print_fault(&soap, stderr);
exit(-1);
}
fprintf(stderr, "Socket connection successful: master socket = %d\n", m);
for ( ; ; )
{
s = soap_accept(&soap);
fprintf(stderr, "Socket connection successful: slave socket = %d\n", s);
if (!soap_valid_socket(s))
{
soap_print_fault(&soap, stderr);
exit(-1);
}
soap_serve(&soap);
soap_end(&soap);
}
}
return 0;
} int add(struct soap *soap, int a, int b, int *result)
{
*result = a + b;
return SOAP_OK;
} int sub(struct soap *soap, int a, int b, int *result)
{
*result = a - b;
return SOAP_OK;
}
编译命令:gcc -o ser server.c soapC.c soapServer.c stdsoap2.c 生成可执行程序 ser
编写自己的客户端。client.c
#include "soapH.h"
#include "soap.nsmap" const char server[] = "http://localhost:8888"; int main(int argc, char *argv[])
{
struct soap soap;
int a, b, result; if (argc < 4)
{
fprintf(stderr, "Usage: [add|sub|mul|div|pow] num num\n");
exit(0);
}
a = atoi(argv[2]);
b = atoi(argv[3]); soap_init(&soap);
switch(*argv[1])
{
case 'a':
//soap_call_ns__add(&soap, server, "", a, b, &result);
soap_call_add(&soap, server, "", a, b, &result);
break;
case 's':
//soap_call_ns__sub(&soap, server, "", a, b, &result);
soap_call_sub(&soap, server, "", a, b, &result);
break;
default:
fprintf(stderr, "Unknown command\n");
exit(0);
}
if (soap.error)
{
soap_print_fault(&soap, stderr);
exit(1);
}
else
printf("result = %d\n", result);
soap_destroy(&soap);
soap_end(&soap);
soap_done(&soap); return 0;
}
编译命令:gcc -o cle client.c soapC.c soapClient.c stdsoap2.c生成可执行程序 cle
测试:
在一个窗口执行:./ser 8888
[root@Logcen5 test]# ./ser 8888
Socket connection successful: master socket = 3
另外一个窗口执行:./cle add 2 3
[root@Logcen5 test]# ./cle add 2 3
result = 5
gsoap使用的更多相关文章
- gsoap设置超时
1.修改gsoap自动生成的代码才能进行超时设置(我这边访问web service的代码都是gsoap工具自动生成.根据wsdl接口) 2.找到生成的soapwwwsdlBindingProxy.cp ...
- gSOAP MTOM
前言 需要准备的知识:wsdl,soap,gSOAP,C++,fidder. 首先介绍几个相关的概念 1.MTOM基础概念 MTOM(Message Transmission Optimiz ...
- gsoap框架下的onvif程序流程分析
SOAP_FMAC5 int SOAP_FMAC6 soap_serve(struct soap *soap) { do { unsigned int k = soap->max_keep_al ...
- 基于gSOAP使用头文件的C语言版web service开发过程例子
基于gSOAP使用头文件的C语言版web service开发过程例子 一服务端 1 打开VS2005,创建一个工程,命名为calcServer. 2 添加一个头文件calc.h,编辑内容如下: 1// ...
- gSoap的 “error LNK2001: 无法解析的外部符号 _namespaces”解决方法
gSoap是C/C++开发webService服务第三方的公开类库. 出现上述错误是因为缺少必要的头文件导致的. 在用wsdl2h生成头文件的时候,一并生成了类似 xx.nsmap 的文件,这个文件实 ...
- 在Windows下用gSoap实现简单加法实例
实现一个简单的a+b程序,在服务器端写一个程序,里面包含了a+b的函数,然后通过客户端代码向其发送两个数字,在服务器运算得到结果返回给客户端显示出来. 1.在gSoap的官网上下载文件夹,本人的版本是 ...
- [转贴]gsoap使用心得!
最近换了个工作环境,现在在大望路这边上班,呵,刚上班接到的任务就是熟悉gsoap!废话少说,现在开始gSoap学习! gSOAP是一个夸平台的,用于开发Web Service服务端和客户端的工具,在W ...
- [转贴]Windows下gSoap交叉编译环境的搭建
本人直接就用过gSoap,它是用以C/C++写webservice的利器 交叉编译的时候,有两个很关键的程序: soapcpp2.exe wsdl2h.exe ...
- 使用GSoap开发WebService客户端与服务端
Gsoap 编译工具提供了一个SOAP/XML 关于C/C++ 语言的实现, 从而让C/C++语言开发web服务或客户端程序的工作变得轻松了很多. 用gsoap开发web service的大致思路 我 ...
- gSoap实现ONVIF中xsd__anyType到具体结构类型的转换
上一篇文章已经粗略计划要讨论gsoap关于序列化/解析编程. 本文则阐述一下关于gsoap生成代码的一些重要特征方法及使用.如题,下我们从ONVIF生成的C码中,挑选简单的一个类型来试验一下与xsd_ ...
随机推荐
- AutoCad 二次开发 .net 之层表的增加 删除 修改图层颜色 遍历 设置当前层
AutoCad 二次开发 .net 之层表的增加 删除 修改图层颜色 遍历 设置当前层 AutoCad 二次开发 .net 之层表的增加 删除 修改图层颜色 遍历 设置当前层我理解的图层的作用大概是把 ...
- Hibernate一对多、多对一的关系表达
一.关系表达: 1.一对多.多对一表的关系: 学生表: 班级表: 在学生表中,学生的学号是主键.在班级表中,班级号是主键,因此,学生表的外键是classno.因此,班级对应学生是一对多,学生对应班级是 ...
- len、is、==、可变于不可变类型
a="asdfghjkl;'iuygb" b="小米" c=['a','b','c'] d= {'name':1,'age':24} # len统计字符或元素的 ...
- iOS定位--CoreLocation
一:导入框架 #import <CoreLocation/CoreLocation.h> 二:设置代理及属性 <CLLocationManagerDelegate> @prop ...
- Docker从入门到实践(1)
一.Docker简介 1.1.什么是 Docker Docker 最初是 dotCloud 公司创始人 Solomon Hykes 在法国期间发起的一个公司内部项目,它是基于 dotCloud 公司多 ...
- C#实现整型数据字任意编码任意进制的转换和逆转换
实现如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespa ...
- python:正则1
鱼c(扩展阅读): Python3 如何优雅地使用正则表达式(详解一) Python3 如何优雅地使用正则表达式(详解二) Python3 如何优雅地使用正则表达式(详解三) Python3 如何优雅 ...
- CSS RESET —— 浏览器样式重置
CSS Reset 1. CSS Reset为什么存在? 只要您的客户存在使用不同浏览器(ie,firefox,chrome等)的可能,那你就不得不从完美的理想状态回到现实,因为不同核心的浏览器对CS ...
- 逆向分析objc,所有类的信息都能在动态调试中获取。
因为objc是动态绑定的,程序运行时必须知道如何绑定,依靠的就是类描述.只要知道类描述是如何组织的就可以获取一切有用的信息.不知道是幸运还是不幸,这些信息全部都在运行的程序中.即使没有IDA这样的工具 ...
- UCACO刷题
UCACO刷题 SUBMIT: /* ID: your_id_here LANG: C++ TASK: test */ 文件:freopen(“file.in", "r" ...