gsoap_2.8.33.zip安装与编译

来源:互联网 发布:python google语音api 编辑:程序博客网 时间:2024/06/05 18:46

一、gSOAP简介
gSOAP一种跨平台的C和 C++软件开发工具包。生成C/C++的RPC代码,XML数据绑定,对SOAP Web服务和其他应用形成高效的具体架构解析器,它们都受益于一个XML接口。 这个工具包提供了一个全面和透明的XML数据绑定解决方案,Autocoding节省大量开发时间来执行SOAP/XML Web服务中的C/C++。此外,使用XML数据绑定大大简化了XML自动映射。应用开发人员不再需要调整应用程序逻辑的具体库和XML为中心的数据,如 交涉DOM。
二、gSOAP安装
1、准备工作
操作系统:centos6.5
位数:64位
gSOAP下载地址:http://sourceforge.net/projects/gsoap2/files/
本文gSOAP为:gsoap_2.8.33.zip
2、解压源码

#unzip gsoap_2.8.33.zip
  • 1

3、配置

#cd gsoap-2.8#./configure --prefix=/usr/local/gSOAP
  • 1
  • 2

4、编译安装

#make
  • 1

三、make出错处理
(1)ylwrap: line 176: yacc: command not found。
yacc是一个生成语法分析器的工具。

 #yum install byacc
  • 1

(2)missing: line 81: flex: command not found。

#yum install flex
  • 1

(需重新配置安装路径)
(3)/usr/bin/ld: cannot find -ly

#yum install yum install bison-devel
  • 1

(4)../../gsoap/stdsoap2.h:690:19:error:zlib.h:No such file or directory

#yum install zlib-devel
  • 1

(5)error: openssl/bio.h: No such file or directory

#yum install openssl-devel
  • 1

(6)undefined reference to soap_ssl_init'
出现该错误,回到上级目录,删除我们解压的目录,重新解压,执行第二步gSOAP安装,make后未报错误,执行

#make  install
  • 1

这里写图片描述
安装成功,会在/usr/local/gSOAP目录下生成我们需要的文件
下载相应的包,主要有2个工具和源代码:
wsdl2h -o outfile.h infile.wsdl 实现wsdl文件到h文件的数据映射
soapcpp2 -c outfile.h生成相应的底层通信stub,strech程序

四、简单测试程序
1、头文件
下面这个简单的例子实现的是在客户端输入2个数字,然后远程调用服务端的加法函数,乘法函数,最后返回结果给客户端。
在这里我们不需要wsdl的文件,可以直接从.h文件来生成代码。我们新建一个文件夹soap,我们定义一个函数声明文件,用来定义接口函数,名称为add.h,内容如下:

//gsoapopt cw//gsoap ns2 schema namespace: urn:add//gsoap ns2 schema form: unqualified//gsoap ns2 service name: add//gsoap ns2 service type: addPortType   add http://schemas.xmlsoap.org/soap/encoding///gsoap ns2  service method-action:     add ""int ns2__add( int num1, int num2, int* sum );int ns2__sub( int num1, int num2, int *sub);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

然后我们执行soapcpp2 -c add.h,自动生成一些远程调用需要的文件。由于我们生成在/usr/local/gSOAP目录下,因此

#/usr/local/gSOAP/bin/soapcpp2 -c add.h
  • 1

2、服务端,创建文件addserver.c

#include <string.h>#include <stdio.h>#include "soapH.h"#include "add.nsmap"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", argv[0]);        exit(1);    } else {    m = soap_bind(&add_soap, NULL, atoi(argv[1]), 100);        if (m < 0) {            soap_print_fault(&add_soap, stderr);            exit(-1);      }     fprintf(stderr, "Socket connection successful: master socket = %d\n", m);     for (;;) {          s = soap_accept(&add_soap);          if (s < 0) {              soap_print_fault(&add_soap, stderr);              exit(-1);       }     fprintf(stderr, "Socket connection successful: slave socket = %d\n", s);     soap_serve(&add_soap);     soap_end(&add_soap);        }    }    return 0;}int ns2__add(struct soap *add_soap, int num1, int num2, int *sum){    *sum = num1 + num2;    return 0;}int ns2__sub(struct soap *sub_soap, int num1, int num2, int *sub){    *sub = num1 - num2;    return 0;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

3、客户端,文件addclient.c

#include <string.h>#include <stdio.h>#include "soapStub.h"#include "add.nsmap"int add(const char *server, int num1, int num2, int *sum);int sub(const char *server, int num1, int num2, int *sub);int main(int argc, char **argv){    int result = -1;    char server[128] = {0};    int num1;    int num2;    int sum;    if (argc < 4) {        printf("usage: %s <ip:port> num1 num2 /n", argv[0]);        exit(1);    }    strcpy(server,argv[1]);    num1 = atoi(argv[2]);    num2 = atoi(argv[3]);    result = add(server, num1, num2, &sum);    if (result != 0) {        printf("soap error, errcode=%d\n", result);    } else {        printf("%d + %d = %d\n", num1, num2, sum);    }    result = sub(server, num1, num2, &sum);    if (result != 0) {        printf("soap error, errcode=%d\n", result);    } else {        printf("%d - %d = %d\n", num1, num2, sum);    }    return 0;}int 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, num1, num2);    if (add_soap.error) {        printf("soap error: %d, %s, %s\n", 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;}int sub(const char *server, int num1, int num2, int *sub){    struct soap add_soap;    int result = 0;    soap_init(&add_soap);    soap_set_namespaces(&add_soap, namespaces);    soap_call_ns2__sub(&add_soap, server, NULL, num1, num2, sub);    printf("server is %s, num1 is %d, num2 is %d\n", server, num1, num2);    if (add_soap.error) {       printf("soap error: %d, %s, %s\n", 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;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71

到此为止,我们自己的代码已经编写完毕,现在我们来编译服务端和客户端
注意:编译的时候我们需要gsoap包里的源代码文件,把stdsoap2.c和stdsoap2.h文件拷贝到当前目录

#cp /opt/gsoap-2.8/gsoap/stdsoap2.c stdsoap2.h ./
  • 1

4、编写Makefile

GSOAP_ROOT = /root/gsoap-2.8/gsoapWSNAME = addCC = g++ -g -DWITH_NONAMESPACESINCLUDE = -I $(GSOAP_ROOT)SERVER_OBJS =soapC.o stdsoap2.o soapServer.o $(WSNAME)server.o CLIENT_OBJS =soapC.o stdsoap2.o soapClient.o $(WSNAME)client.o all: serverserver: $(SERVER_OBJS)     $(CC) $(INCLUDE) -o $(WSNAME)server $(SERVER_OBJS)    client: $(CLIENT_OBJS)     $(CC) $(INCLUDE) -o $(WSNAME)client $(CLIENT_OBJS)clean:    rm -f *.o *.xml *.a *.wsdl *.nsmap soap* $(WSNAME)Stub.* $(WSNAME)server ns.xsd $(WSNAME)test
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

5、编译

#make server#make client
  • 1
  • 2

编译生成服务端执行程序addserver和客户端执行程序addclient
6、执行
(1)在终端上执行服务器程序

#./addserver  8888
  • 1

终端打印出“Socket connection successful: master socket = 3”,那么你的server已经在前台run起来了
(2)新建另一终端执行客户端程序

#./addclient http://localhost:8888 99 22
  • 1
[root@localhost gsoap]# ./addclient  http://localhost:8888 99 22server is http://localhost:8888, num1 is 99, num2 is 2299 + 22 = 121server is http://localhost:8888, num1 is 99, num2 is 2299 - 22 = 77[root@localhost gsoap]# 
原创粉丝点击