一. 安装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. 每天3分钟操作系统修炼秘籍(14):IO操作和DMA、RDMA

    点我查看秘籍连载 I/O操作和DMA.RDMA 用户进程想要执行IO操作时(例如想要读磁盘数据.向磁盘写数据.读键盘的输入等等),由于用户进程工作在用户模式下,它没有执行这些操作的权限,只能通过发起对 ...

  2. 使用CXF发布webservice服务及注意要点

    一.概念 1.什么是webservice Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML标准来描述.发布.发现.协调和配置这些应用程序,用 ...

  3. C# 获取系统当前登录用户(管理员身份运行同样有效)

    今天学习下怎么用.Net获取系统当前登陆用户名,因为目前网上基本只有最简单的方式,但以管理员身份运行的话就会获取不到,所以特整理一下作为分享,最后附带参考文档,方便深究的童鞋继续学习. ======= ...

  4. T-SQL Part XII: Access Remote SQL Server

    要链接远程的SQL Server,需要一下几个步骤(以下的步骤都是在远程系统上进行): 确认远程SQL Server所监听的端口号 官方的文档是使用SQL Server Configuration M ...

  5. poj 3281 Dining (Dinic)

    Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22572   Accepted: 10015 Descript ...

  6. Java开发者入职必备条件

    01.基础技术体系 我认为知识技能体系化是判断技术是否过关的第一步.知识体系化包含两层含义: 1. 能够知道技术知识图谱(高清版图谱扫文末二维码)的内容 比如分布式系统中常用的RPC技术,其背后就涉及 ...

  7. MySQL/MariaDB读写分离配置

    DB读写分离描述 数据库的读写分离其实就是为了加减少数据库的压力:数据库的写入操作由主数据库来进行,读取操作由从数据库来进行操作.实现数据库读写分离技术是有很多方法的,在这里我就用一个比较简单的mys ...

  8. 力扣(LeetCode)整数形式的整数加法 个人题解

    对于非负整数 X 而言,X 的数组形式是每位数字按从左到右的顺序形成的数组.例如,如果 X = 1231,那么其数组形式为 [1,2,3,1]. 给定非负整数 X 的数组形式 A,返回整数 X+K 的 ...

  9. python:模块1——标准库简介

    一.文档 windows系统:IDLE中打开帮助文档 Tutorial:简单入门 Library Reference:python内置函数和标准库(看不完的,当做字典来查)(此外还有pypi(拍派社区 ...

  10. [机器学习笔记]kNN进邻算法

    K-近邻算法 一.算法概述 (1)采用测量不同特征值之间的距离方法进行分类 优点: 精度高.对异常值不敏感.无数据输入假定. 缺点: 计算复杂度高.空间复杂度高. (2)KNN模型的三个要素 kNN算 ...