一、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
3、配置
#cd gsoap-2.8
#./configure --prefix=/usr/local/gSOAP
4、编译安装
#make
三、make出错处理
(1)
ylwrap: line 176: yacc: command not found。
yacc是一个生成语法分析器的工具。
(2)
missing: line 81: flex: command not found。
#yum install flex
(需重新配置安装路径)
(3)
/usr/bin/ld: cannot find -ly
#yum install yum install bison-devel
(4)
../../gsoap/stdsoap2.h:690:19:error:zlib.h:No such file or directory
#yum install zlib-devel
(5)
error: openssl/bio.h: No such file or directory
#yum install openssl-devel
(6)
undefined reference to soap_ssl_init'
出现该错误,回到上级目录,删除我们解压的目录,重新解压,执行第二步gSOAP安装,make后未报错误,执行
#make install
安装成功,会在/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,内容如下:
add http:
int ns2__add( int num1, int num2, int* sum );
int ns2__sub( int num1, int num2, int *sub);
然后我们执行soapcpp2 -c add.h,自动生成一些远程调用需要的文件。由于我们生成在/usr/local/gSOAP目录下,因此
#/usr/local/gSOAP/bin/soapcpp2 -c add.h
2、服务端,创建文件addserver.c
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 /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
", 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
", 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;
}
3、客户端,文件addclient.c
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 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
", result);
} else {
printf("%d + %d = %d
", num1, num2, sum);
}
result = sub(server, num1, num2, &sum);
if (result != 0) {
printf("soap error, errcode=%d
", result);
} else {
printf("%d - %d = %d
", 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
", server, num1, num2);
if (add_soap.error) {
printf("soap error: %d, %s, %s
", 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
", server, num1, num2);
if (add_soap.error) {
printf("soap error: %d, %s, %s
", 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;
}
到此为止,我们自己的代码已经编写完毕,现在我们来编译服务端和客户端
注意:编译的时候我们需要gsoap包里的源代码文件,把stdsoap2.c和stdsoap2.h文件拷贝到当前目录
#cp /opt/gsoap-2.8/gsoap/stdsoap2.c stdsoap2.h ./
4、编写Makefile
GSOAP_ROOT = /root/gsoap-2.8/gsoap
WSNAME = add
CC = g++ -g -DWITH_NONAMESPACES
INCLUDE = -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: server
server: $(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
5、编译
#make server
#make client
编译生成服务端执行程序addserver和客户端执行程序addclient
6、执行
(1)在终端上执行服务器程序
#./addserver 8888
终端打印出“Socket connection successful: master socket = 3”,那么你的server已经在前台run起来了
(2)新建另一终端执行客户端程序
#./addclient http:
[root@localhost gsoap]
server is http://localhost:8888, num1 is 99, num2 is 22
99 + 22 = 121
server is http://localhost:8888, num1 is 99, num2 is 22
99 - 22 = 77
[root@localhost gsoap]