一. 安装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使用的更多相关文章

  1. gsoap设置超时

    1.修改gsoap自动生成的代码才能进行超时设置(我这边访问web service的代码都是gsoap工具自动生成.根据wsdl接口) 2.找到生成的soapwwwsdlBindingProxy.cp ...

  2. gSOAP MTOM

    前言 需要准备的知识:wsdl,soap,gSOAP,C++,fidder. 首先介绍几个相关的概念 1.MTOM基础概念      MTOM(Message Transmission Optimiz ...

  3. gsoap框架下的onvif程序流程分析

    SOAP_FMAC5 int SOAP_FMAC6 soap_serve(struct soap *soap) { do { unsigned int k = soap->max_keep_al ...

  4. 基于gSOAP使用头文件的C语言版web service开发过程例子

    基于gSOAP使用头文件的C语言版web service开发过程例子 一服务端 1 打开VS2005,创建一个工程,命名为calcServer. 2 添加一个头文件calc.h,编辑内容如下: 1// ...

  5. gSoap的 “error LNK2001: 无法解析的外部符号 _namespaces”解决方法

    gSoap是C/C++开发webService服务第三方的公开类库. 出现上述错误是因为缺少必要的头文件导致的. 在用wsdl2h生成头文件的时候,一并生成了类似 xx.nsmap 的文件,这个文件实 ...

  6. 在Windows下用gSoap实现简单加法实例

    实现一个简单的a+b程序,在服务器端写一个程序,里面包含了a+b的函数,然后通过客户端代码向其发送两个数字,在服务器运算得到结果返回给客户端显示出来. 1.在gSoap的官网上下载文件夹,本人的版本是 ...

  7. [转贴]gsoap使用心得!

    最近换了个工作环境,现在在大望路这边上班,呵,刚上班接到的任务就是熟悉gsoap!废话少说,现在开始gSoap学习! gSOAP是一个夸平台的,用于开发Web Service服务端和客户端的工具,在W ...

  8. [转贴]Windows下gSoap交叉编译环境的搭建

    本人直接就用过gSoap,它是用以C/C++写webservice的利器     交叉编译的时候,有两个很关键的程序:         soapcpp2.exe         wsdl2h.exe ...

  9. 使用GSoap开发WebService客户端与服务端

    Gsoap 编译工具提供了一个SOAP/XML 关于C/C++ 语言的实现, 从而让C/C++语言开发web服务或客户端程序的工作变得轻松了很多. 用gsoap开发web service的大致思路 我 ...

  10. gSoap实现ONVIF中xsd__anyType到具体结构类型的转换

    上一篇文章已经粗略计划要讨论gsoap关于序列化/解析编程. 本文则阐述一下关于gsoap生成代码的一些重要特征方法及使用.如题,下我们从ONVIF生成的C码中,挑选简单的一个类型来试验一下与xsd_ ...

随机推荐

  1. 二、安装JDK - Java软件的安装

    jdk是 Java 语言的软件开发工具包,主要用于移动设备.嵌入式设备上的java应用程序. 1.安装包的下载:http://pan.baidu.com/s/1mgh58ve (该安装包是绿色的,解压 ...

  2. ssm整合的登录

    新建一个web工程,主要结构如下: 数据库创建如下: 控制层的代码FormController 类 package codeRose.controller; import org.springfram ...

  3. it公司比较

    1:本人西电通院2013届毕业硕士,根据今年找工作的情况以及身边同学的汇总,总结各大公司的待遇如下,吐血奉献给各位学弟学妹,公司比较全,你想去的公司不在这里面,基本上是无名小公司了:但无名小公司有时也 ...

  4. 查看redis占用内存大小的方法

    查看redis占用内存大小的方法 <pre>redis-cli auth 密码info</pre><pre># Memory used_memory:1349009 ...

  5. 如何查看当前linux服务器是否支持虚拟化

    [root@localhost ~]# grep -E '(svm|vmx)' /proc/cpuinfo 或者: [root@localhost ~]# cat /proc/cpuinfo 找到fl ...

  6. Flutter之环境配置与项目搭建

    Flutter之环境配置与项目搭建 一,介绍 1.1,Dart Dart 是一种 易于学习. 易于扩展.并且可以部署到 任何地方 的 应用 编程 语言.并且同时借鉴了Java和JavaScript.D ...

  7. thinkphp volist标签中加if判断的写法

    <if condition="$vo['devstatus'] eq 1">在线<else /> 离线</if> IF标签用法 <if c ...

  8. thinkphp 比RBAC更好的权限认证方式(Auth类认证)

    Auth 类已经在ThinkPHP代码仓库中存在很久了,但是因为一直没有出过它的教程, 很少人知道它, 它其实比RBAC更方便 . RBAC是按节点进行认证的,如果要控制比节点更细的权限就有点困难了, ...

  9. mysql提示Packet for query is too large (1142 > 1024)解决方案

    注:最近mysql一直提示如下错误 Packet for query is too large (1185 > 1024). You can change this value on the s ...

  10. python_09

    今日内容: scrapy各组件 Components: 1.引擎(EGINE) 引擎负责控制系统所有组件之间的数据流,并在某些动作发生时触发事件.有关详细信息,请参见上面的数据流部分. 2.调度器(S ...