一、移植
1、下载源码
http://sourceforge.net/projects/gsoap2/files/
本例下载最新版本为:gsoap_2.8.33.zip
2、解压源码
$ unzip gsoap_2.8.33.zip
3、平台准备unbuntu1204
$sudo apt-get install build-essential
$sudo apt-get install libgtk2.0-dev libglib2.0-dev
$sudo apt-get install checkinstall
$sudo apt-get install flex bison
$sudo apt-get install openssl
$sudo apt-cache search libssl | grep SSL
$sudo apt-get install libssl-dev
4、编译x86 平台下工具
$ ./configure --prefix=/usr/local/gsopa-x86
$ make
$ make install
$ export PATH=/usr/local/gSOAP-x86/bin:$PATH
5、编译arm平台工具
./configure --prefix=/usr/local/gsopa-arm --host=arm-linux
再次配置源码,这次配置目标平台变为arm-linux
修改源码,根据编译错误提示进行适当修改:
修改源码目录下的config.h
注释掉:
#define malloc rpl_malloc
soapcpp和wsdl2h在编译过程中会生成同事这两个工具也在编译时会被用来生成一些特定的文件,所以如果soapcpp和wsdl2h如果被编译成arm架构则无法在PC上执行,也就意味着接下的编译无法完成,所以这里选择不编译这两个文件,而选择使用上节编译所生产的x86架构的对应文件。
修改gsoap/src/Makefile
注释掉
soapcpp2$(EXEEXT): $(soapcpp2_OBJECTS) $(soapcpp2_DEPENDENCIES) $(EXTRA_soapcpp2_DEPENDENCIES)
@rm -f soapcpp2$(EXEEXT)
$(AM_V_CCLD)$(soapcpp2_LINK) $(soapcpp2_OBJECTS) $(soapcpp2_LDADD) $(LIBS)
修改gsoap/wsdl/Makefile
wsdl2h$(EXEEXT): $(wsdl2h_OBJECTS) $(wsdl2h_DEPENDENCIES) $(EXTRA_wsdl2h_DEPENDENCIES)
@rm -f wsdl2h$(EXEEXT)
$(AM_V_CXXLD)$(wsdl2h_LINK) $(wsdl2h_OBJECTS) $(wsdl2h_LDADD) $(LIBS)
拷贝x86架构下的soapcpp和wsdl2h到工程源码目录下:
$ cp /home/linux/gsoap/gsoap-x86/bin/soapcpp2 /home/linux/gsoap/gsoap-2.8/gsoap/src/
$ cp /home/linux/gsoap/gsoap-x86/bin/wsdl2h /home/linux/gsoap/gsoap-2.8/gsoap/wsdl/
编译
$ make
安装
$ make install
二 程序
1、代码
add.h
点击(此处)折叠或打开
//gsoapopt cw
//gsoap ns2 schema namespace: urn:add
//gsoap ns2 schemaform: unqualified
//gsoap ns2 service name: add
//gsoap ns2 service type: addPortType
//gsoap ns2 service port:http://websrv.cs.fsu.edu/~engelen/addserver.cgi
//gsoap ns2 service namespace: urn:add
//gsoap ns2 service transport: http://schemas.xmlsoap.org/soap/http
//gsoap ns2 service method-style: add rpc
//gsoap ns2 service method-encoding: add http://schemas.xmlsoap.org/soap/encoding/
//gsoap ns2 service method-action: add""
int ns2__add(int num1, int num2, int* sum);addserver.c
点击(此处)折叠或打开
#include "soapH.h"
#include "add.nsmap"
int main(int args,char**argv)
{
int m,s;
struct soap add_soap;
soap_init(&add_soap);
soap_set_namespaces(&add_soap,namespaces);
if(args<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\n",m);
for(;;)
{
s = soap_accept(&add_soap);
if(s<0)
{
soap_print_fault(&add_soap,stderr);
exit(-1);
}
fprintf(stderr,"socket connecntion 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;
}addclient.c
点击(此处)折叠或打开
#include "soapStub.h"
#include "add.nsmap"
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;
}addtest.c
点击(此处)折叠或打开
#include
#include
#include
int add(const char*server,int num1,int num2,int*sum);
int main(int argc,char**argv)
{
int resutl = -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]);
resutl = add(server,num1,num2,&sum);
if(resutl!=0)
{
printf("soap error,errorcode=%d\n",resutl);
}
else
{
printf("%d+%d=%d\n",num1,num2,sum);
}
return 0;
}//x86 linux Makefile
点击(此处)折叠或打开
GSOAP_ROOT=/usr/local/gSOAP-x86
WSNAME=add
CC= g++ -g -DWITH_NONAMESPACES
INCLUDE=-I $(GSOAP_ROOT)/include
SERVER_OBJS = soapC.o stdsoap2.o soapServer.o addserver.o
CLIENT_OBJS = soapC.o stdsoap2.o soapClient.o addclient.o addtest.o
all:server
server: $(SERVER_OBJS)
$(CC) $(INCLUDE) -o addserver $(SERVER_OBJS)
client: $(CLIENT_OBJS)
$(CC) $(INCLUDE) -o addtest $(CLIENT_OBJS)
cl:
rm -f *.o *.xml *.a *.wsdl *.nsmap soapH.h $(WSNAME)Stub.* $(WSNAME)server ns.xsd $(WSNAME)test//arm-linux Makefile
点击(此处)折叠或打开
GSOAP_ROOT=/usr/local/gSOAP-arm
WSNAME=add
CC= arm-linux-g++ -g -DWITH_NONAMESPACES
INCLUDE=-I $(GSOAP_ROOT)/include
SERVER_OBJS = soapC.o stdsoap2.o soapServer.o addserver.o
CLIENT_OBJS = soapC.o stdsoap2.o soapClient.o addclient.o addtest.o
all:server
server: $(SERVER_OBJS)
$(CC) $(INCLUDE) -o addserver $(SERVER_OBJS)
client: $(CLIENT_OBJS)
$(CC) $(INCLUDE) -o addtest $(CLIENT_OBJS)
cl:
rm -f *.o *.xml *.a *.wsdl *.nsmap soapH.h $(WSNAME)Stub.* $(WSNAME)server ns.xsd $(WSNAME)test 2、编译
先
$soapcpp2 -c add.h 产生soap文件
$make client 得到客户端程序
$make all 得到服务端程序
(可分别编译X86 与 ARM板程序,仅仅是执行位置不一样,编译后程序不依赖任何库,可在任意目录执行)
3、执行
服务器端:$./addserver 8000 该服务端口绑定为8000端口
成功:Socket connection successful:master socket=3
客服端连接成功:socket connecntion successful:slave socket = 4
客服端:$./addclient 192.168.7.7 8000 1 2
server is 192.168.7.7:8000,num1 is 1,num2 is 2 1+2=3
这里服务器IP地址,加端口号,加 SOAP参数
三、WSDL天气获取
1、新建目录
$weather
2、从服务器下载头文件
$wsdl2h -c -o weather.h http://www.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
//可ping是否通
3、生成客户代码
$soapcpp2 -x -C -c weather.h
-x不生成xml文件
-C 仅仅生存客服端代码
4.拷贝gsoap目录下的stdsoap2.h stdsoap2.c 到weather目录下
5、编写客户端程序
// 数据结构参考了weather.h和http://www.webxml.com.cn/files/WeatherWsHelp.pdf
点击(此处)折叠或打开
#include
#include "soapH.h"
#include
#include "WeatherWSSoap.nsmap"
//author: jon/2016/08/01
#define CITY_NAME "深圳"
int main(int argc,char*argv[])
{
struct soap soap;
int ret = 0;
int element= 0;
int i=0;
struct _ns1__getWeather myCitWeather;
myCitWeather.theCityCode = CITY_NAME;
//myCitWeather.theUserID= 0;
struct _ns1__getWeatherResponse myRetnWeather;
soap_init(&soap);
soap_set_mode(&soap,SOAP_C_UTFSTRING);
soap.mode |=SOAP_C_UTFSTRING;
if(soap_call___ns1__getWeather(&soap,NULL,NULL,&myCitWeather,&myRetnWeather)==SOAP_OK)
{
int Size = myRetnWeather.getWeatherResult->__sizestring;
for(i=0;i{
printf("is %d get %s\n",i,myRetnWeather.getWeatherResult->string[i]);
}
}
soap_destroy(&soap);
soap_end(&soap);
soap_done(&soap);
return 0;
}
6.makefile
#CROSS_COMPILER=
CC:=$(CROSS_COMPILER)gcc
GCC:=$(CROSS_COMPILER)g++
all:
$(CC) -o weather weather.c soapC.c soapClient.c stdsoap2.c
clean:
rm -f weather
7.执行
$make
$./weather
is 0 get 直辖市 北京
is 1 get 北京
is 2 get 792
is 3 get 2016/08/01 10:31:54
is 4 get 今日天气实况:气温:28℃;风向/风力:北风 2级;湿度:73%
is 5 get 紫外线强度:弱。空气质量:较差。
is 6 get 紫外线指数:弱,辐射较弱,涂擦SPF12-15、PA+护肤品。
感冒指数:少发,感冒机率较低,避免长期处于空调屋中。
穿衣指数:热,适合穿T恤、短薄外套等夏季服装。
洗车指数:较适宜,无雨且风力较小,易保持清洁度。
运动指数:较适宜,较适宜进行各种户内外运动。
空气污染指数:较差,气象条件较不利于空气污染物扩散。。
is 7 get 8月1日 阴
is 8 get 24℃/31℃
is 9 get 无持续风向微风
is 10 get 2.gif
is 11 get 2.gif
is 12 get 8月2日 晴转多云
is 13 get 24℃/33℃
is 14 get 无持续风向微风
is 15 get 0.gif
is 16 get 1.gif
is 17 get 8月3日 晴转多云
is 18 get 25℃/33℃
is 19 get 无持续风向微风
is 20 get 0.gif
is 21 get 1.gif
is 22 get 8月4日 多云转晴
is 23 get 24℃/31℃
is 24 get 无持续风向微风
is 25 get 1.gif
is 26 get 0.gif
is 27 get 8月5日 晴转多云
is 28 get 25℃/31℃
is 29 get 无持续风向微风
is 30 get 0.gif
is 31 get 1.gif
欢迎关注并加入物联网行业联盟,积累行业人脉和资源。