1、下载gSOAP
在gSOAP官网中可以找到最新的gSOAP安装包以及相关的文档
gSOAP官网:http://www.cs.fsu.edu/~engelen/soap.html
gSOAP下载地址:http://sourceforge.net/projects/gsoap2/
2、安装gSOAP
a、解压zip压缩包
命令:unzip gsoap_2.7.17.zip
b、进入解压后生成的文件夹gsoap-2.7
命令:cd gsoap-2.7/
c、切换到root用户
命令:su
输入密码
d、配置编译环境
在gsoap-2.7/文件夹下有一个configure文件,执行它,自动配置编译环境
命令:./configure
e、编译连接
命令:make
f、安装gSOAP
命令:make install
可用wsdl2h或者soapcpp2查看gSOAP是否已经安装成功
用命令whereis gsoap查看gSOAP安装到的位置
3、编写calc.h文件
a、新建一个文件夹calc
命令:mkdir calc
b、进入calc
命令:cd calc
c、创建文件calc.h,在文件中写入以下内容,保存
//gsoap ns service name: calc Simple calculator service
//gsoap ns service style: rpc
//gsoap ns service encoding: encoded
//gsoap ns service namespace: http://localhost/calc.wsdl
//gsoap ns service location: http://localhost/server.cgi
//gsoap ns schema namespace: urn:calc
int ns__add(double a, double b, double *result);
int ns__sub(double a, double b, double *result);
int ns__mul(double a, double b, double *result);
int ns__div(double a, double b, double *result);
int ns__pow(double a, double b, double *result);
注意:
a、以上的几行注释中,gsoap ns service不能删除,但可以修改
b、ns__add等的ns后面是两个下划线
4、调用soapcpp2自动生成相关的文件
(为了方便代码管理,在calc文件夹下新建一个soap)
当前工作目录为calc/soap
命令:soapcpp2 -i ../calc.h
注意:
可能会报错,一般是不知道gsoap相关include文件的位置,可以使用
命令soapcpp2 -i ../calc.h -I /usr/include/gsoap
5、编写服务器端代码:server.cpp
当前工作目录:calc
#include "soap/soapcalcService.h"
#include <iostream>
using namespace std;
int main(int argc, char **argv) {
calcService calc;
int port = 65531;
if (calc.run(port)) {
cerr << "Calc service run failed" << endl;
exit(0);
}
return 0;
}
int calcService::add(double a, double b, double* result) {
*result = a + b;
return SOAP_OK;
}
int calcService::sub(double a, double b, double* result) {
*result = a - b;
return SOAP_OK;
}
int calcService::mul(double a, double b, double* result) {
*result = a * b;
return SOAP_OK;
}
int calcService::div(double a, double b, double* result) {
if (b) {
*result = a / b;
} else {
return SOAP_FAULT;
}
return SOAP_OK;
}
int calcService::pow(double a, double b, double *result) {
*result = ::pow(a, b);
if (soap_errno == EDOM) {
char *s = (char*) soap_malloc(this, 1024);
sprintf(s, "Can't take the power of %f to %f", a, b);
sprintf(s,"<error xmlns=\"http://tempuri.org/\">Can't take power of %f to %f</error>",a, b);
return SOAP_FAULT;
}
return SOAP_OK;
}
6、编写客户端代码:client.cpp
#include "soap/soapcalcProxy.h"
#include "soap/calc.nsmap"
#include <iostream>
using namespace std;
int main(int argc, char **argv) {
double a = 4;
double b = 2;
double result;
calcProxy calc;
calc.soap_endpoint = "http://localhost:65531";
calc.add(a,b,&result);
cout<<a<<"+"<<b<<"="<<result<<endl;
calc.sub(a,b,&result);
cout<<a<<"-"<<b<<"="<<result<<endl;
calc.mul(a,b,&result);
cout<<a<<"*"<<b<<"="<<result<<endl;
calc.div(a,b,&result);
cout<<a<<"/"<<b<<"="<<result<<endl;
calc.pow(a,b,&result);
cout<<a<<"^"<<b<<"="<<result<<endl;
return 0;
}
7、编写Makefile文件:makefile、sources.mk、subdir.mk、soap/subdir.mk、objects.mk
新建bin文件夹
当前工作目录:calc/bin
#文件:makefile
RM := rm -rf
# 引入相关的生成文件
-include sources.mk
-include subdir.mk
-include soap/subdir.mk
-include objects.mk
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(C++_DEPS)),)
-include $(C++_DEPS)
endif
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
ifneq ($(strip $(CC_DEPS)),)
-include $(CC_DEPS)
endif
ifneq ($(strip $(SERVER_CPP_DEPS)),)
-include $(SERVER_CPP_DEPS)
endif
ifneq ($(strip $(CLIENT_CPP_DEPS)),)
-include $(CLIENT_CPP_DEPS)
endif
ifneq ($(strip $(CXX_DEPS)),)
-include $(CXX_DEPS)
endif
ifneq ($(strip $(C_UPPER_DEPS)),)
-include $(C_UPPER_DEPS)
endif
endif
# 生成目标
all: server client
# 相关工具
server: $(SERVER_OBJS) $(USER_OBJS)
@echo '创建目标: $@'
g++ -o"server" $(SERVER_OBJS) $(USER_OBJS) $(LIBS)
@echo '完成创建目标: $@'
@echo "\n"
client: $(CLIENT_OBJS) $(USER_OBJS)
@echo '创建目标: $@'
g++ -o"client" $(CLIENT_OBJS) $(USER_OBJS) $(LIBS)
@echo '完成创建目标: $@'
@echo "\n"
# 清空临时文件
clean:
-$(RM) $(SERVER_OBJS)$(CLIENT_OBJS)$(C++_DEPS)$(C_DEPS)$(CC_DEPS)$(SERVER_CPP_DEPS)$(CLIENT_CPP_DEPS)$(EXECUTABLES)$(CXX_DEPS)$(C_UPPER_DEPS)
-@echo "\n"
cleanall:
-$(RM) $(SERVER_OBJS)$(CLIENT_OBJS)$(C++_DEPS)$(C_DEPS)$(CC_DEPS)$(SERVER_CPP_DEPS)$(CLIENT_CPP_DEPS)$(EXECUTABLES)$(CXX_DEPS)$(C_UPPER_DEPS) server client
-@echo "\n"
cleanexe:
-$(RM) server client
-@echo "\n"
.PHONY: all clean dependents cleanall cleanexe
=====================================================================================================
#文件:sources.mk
O_SRCS :=
SERVER_CPP_SRCS :=
CLIENT_CPP_SRCS :=
C_UPPER_SRCS :=
C_SRCS :=
S_UPPER_SRCS :=
OBJ_SRCS :=
ASM_SRCS :=
CXX_SRCS :=
C++_SRCS :=
CC_SRCS :=
SERVER_OBJS :=
CLIENT_OBJS :=
C++_DEPS :=
C_DEPS :=
CC_DEPS :=
SERVER_CPP_DEPS :=
CLIENT_CPP_SRCS :=
EXECUTABLES :=
CXX_DEPS :=
C_UPPER_DEPS :=
# 子目录
SUBDIRS := \
soap \
. \
=====================================================================================================
#文件:sources.mk
O_SRCS :=
SERVER_CPP_SRCS :=
CLIENT_CPP_SRCS :=
C_UPPER_SRCS :=
C_SRCS :=
S_UPPER_SRCS :=
OBJ_SRCS :=
ASM_SRCS :=
CXX_SRCS :=
C++_SRCS :=
CC_SRCS :=
SERVER_OBJS :=
CLIENT_OBJS :=
C++_DEPS :=
C_DEPS :=
CC_DEPS :=
SERVER_CPP_DEPS :=
CLIENT_CPP_SRCS :=
EXECUTABLES :=
CXX_DEPS :=
C_UPPER_DEPS :=
# 子目录
SUBDIRS := \
soap \
. \
=====================================================================================================
#文件:subdir.mk
# 生成的相关参数
SERVER_CPP_SRCS += \
../server.cpp
SERVER_OBJS += \
./server.o
SERVER_CPP_DEPS += \
./server.d
CLIENT_CPP_SRCS += \
../client.cpp
CLIENT_OBJS += \
./client.o
CLIENT_CPP_DEPS += \
./client.d
# 根据.cpp文件生成.o文件
%.o: ../%.cpp
@echo '创建目标: $<'
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<"
@echo '完成创建目标: $<'
@echo "\n"
=====================================================================================================
#文件:soap/subdir.mk
# 生成的相关参数
SERVER_CPP_SRCS += \
../soap/soapC.cpp \
../soap/soapcalcService.cpp \
/usr/include/gsoap/stdsoap2.cpp
SERVER_OBJS += \
./soap/soapC.o \
./soap/soapcalcService.o \
./soap/stdsoap2.o
SERVER_CPP_DEPS += \
./soap/soapC.d \
./soap/soapcalcService.d \
./soap/stdsoap2.d
CLIENT_CPP_SRCS += \
../soap/soapC.cpp \
../soap/soapcalcProxy.cpp \
/usr/include/gsoap/stdsoap2.cpp
CLIENT_OBJS += \
./soap/soapC.o \
./soap/soapcalcProxy.o \
./soap/stdsoap2.o
CLIENT_CPP_DEPS += \
./soap/soapC.d \
./soap/soapcalcProxy.d \
./soap/stdsoap2.d
# 根据.cpp文件生成.o文件
soap/%.o: ../soap/%.cpp
@echo '创建目标: $<'
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<"
@echo '完成创建目标: $<'
@echo "\n"
soap/%.o:/usr/include/gsoap/%.cpp
@echo '创建目标: $<'
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<"
@echo '完成创建目标: $<'
@echo "\n"
=====================================================================================================
#文件:objects.mk
USER_OBJS :=
LIBS :=
8、编译
当前工作目录:calc/bin
make
9、开启服务端
./server
10、开启客户端
./client
11、文件总览(3个目录, 42个文件)
calc // 根目录
|-- bin // 文件夹,存放makefile,以及生成的.o、.d、可执行文件
| |-- client
| |-- client.d
| |-- client.o
| |-- makefile
| |-- objects.mk
| |-- server
| |-- server.d
| |-- server.o
| |-- soap
| | |-- soapcalcProxy.d
| | |-- soapcalcProxy.o
| | |-- soapcalcService.d
| | |-- soapcalcService.o
| | |-- soapC.d
| | |-- soapC.o
| | |-- stdsoap2.d
| | |-- stdsoap2.o
| | `-- subdir.mk
| |-- sources.mk
| `-- subdir.mk
|-- calc.h // calc.h
|-- client.cpp // 客户端
|-- server.cpp // 服务器端
`-- soap // 该文件夹存放命令soapcpp2 -i ../calc.h生成的文件
|-- calc.add.req.xml
|-- calc.add.res.xml
|-- calc.div.req.xml
|-- calc.div.res.xml
|-- calc.mul.req.xml
|-- calc.mul.res.xml
|-- calc.nsmap
|-- calc.pow.req.xml
|-- calc.pow.res.xml
|-- calc.sub.req.xml
|-- calc.sub.res.xml
|-- calc.wsdl
|-- ns.xsd
|-- soapcalcProxy.cpp
|-- soapcalcProxy.h
|-- soapcalcService.cpp
|-- soapcalcService.h
|-- soapC.cpp
|-- soapH.h
`-- soapStub.h
Linux下gSOAP的使用
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Linux 下 LVM 的基础使用
Linux LVM 基础使用方法
逻辑卷 元数据 自动备份 -
windows下使用开源gSOAP技术调用WebService
大名鼎鼎的gSOAP Toolkit 这里就不多做介绍了,此次的WebService访问即是借
soap #include 头文件 服务访问点