一. 安装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. day 1 晚上 P2824 [HEOI2016/TJOI2016]排序 线段树

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #inclu ...

  2. sshd服务以及基于口令的远程登陆

    ssh用为客户端,主要进行服务器端的连接:sshd用为服务器端 几个常用的命令: systemctl              ##服务控制命令   systemctl start sshd   ## ...

  3. 零基础Linux入门之《Linux就该这么学》

    本书是由全国多名红帽架构师(RHCA)基于最新Linux系统共同编写的高质量Linux技术自学教程,极其适合用于Linux技术入门教程或讲课辅助教材,目前是国内最值得去读的Linux教材,也是最有价值 ...

  4. 「CF630C」Lucky Numbers

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description The numbers of all offices in the new ...

  5. jquery判断手指滑动方向

    jquery判断手指滑动方向 <pre> /*判断哪个滑动方向还是自己改下 要么上下 要么左右*/ var startX; var startY; $(".shanghua&qu ...

  6. HTML创建图像映射,布局,表单

    来源: 实验楼 创建图像映射 在这之前我们动手试验过将图片作为链接来使用,触发链接的方式就是点击图片的任何地方都可以链接到跳转地址,有时我们需要实现,点击图片的不同地方跳转到不同的地方.意思就是,一张 ...

  7. jdk8 函数式编程概念

    yls 2019/11/7 函数式接口 如果一个接口只有一个抽象方法,那么该接口就是函数式接口 如果我们在某接口上声明了FunctionalInterface注解,那么编译器就会按照函数式接口的定义来 ...

  8. JavaScript 运行原理

    i{margin-right:4px;margin-top:-0.2em}.like_comment_tips .weui-icon-success{background:transparent ur ...

  9. 【idea】高德地图可以关爱一下高个汽车

    现状:1.交通事故时不时能看到大卡车,双层巴士在城市里限高区域时的车祸 原因分析:1.司机对路况不熟,驶入新的限高路,造成事故2.司机对车况不熟,临时换的车驾驶,忘记车高的变化3.司机路况车况都熟,道 ...

  10. (C#)WPF:关于INotifyPropertyChanged接口的介绍

    注意:INotifyPropertyChanged接口位于System.CompenentModel名称空间中,想使用INotifyPropertyChanged接口时,头文件需添加“using Sy ...