Linux平台下基于C++语言使用gSOAP开发Web Service服务端和客户端程序
(1)头文件不变,还是SmsWBS.h
//gsoap ns service name: SmsWBS
//gsoap ns service style: rpc
//gsoap ns service namespace: http://192.168.1.102:9000/SmsWBS.wsdl
//gsoap ns service location: http://192.168.1.102:9000
//gsoap ns service encoding: encoded
//gsoap ns schema namespace: urn:SmsWBS
int ns__add(int num1, int num2, int *sum);
(2)编写一个万能的Makefile文件:(此文件稍作改动可以移植到任何工程中)
server := SmsWBSserver
client := SmsWBStest
#your gsoap install directory
GSOAP_DIR=../../gsoap-2.8
#web service name,added by danny
WSNAME0=soapSmsWBS
#compiler
CORSS_COMPILER =
G++ :=$(CORSS_COMPILER)g++
#flags
LIBS:= -lpthread -lm
INCLS := -I ./
CFLAGS := -g -O2 -Wall -Wno-deprecated-declarations
#files
WSDL_FILES := SmsWBS.h
GSOAP_FILES :=$(WSNAME0)Proxy.cpp $(WSNAME0)Proxy.h $(WSNAME0)Service.h $(WSNAME0)Service.cpp soapC.cpp soapH.h soapStub.h
CP_FILES := stdsoap2.h stdsoap2.cpp
COM_SOURCES := soapC.cpp stdsoap2.cpp
COM_OBJS := $(COM_SOURCES:.cpp=.o)
SERVER_SOURCES := $(server).cpp $(WSNAME0)Service.cpp
SERVER_OBJS := $(SERVER_SOURCES:.cpp=.o)
CLIENT_SOURCES := $(client).cpp $(WSNAME0)Proxy.cpp
CLIENT_OBJS := $(CLIENT_SOURCES:.cpp=.o)
TYPEMAP = $(wildcard *typemap.dat)
ifeq ( , $(TYPEMAP))
TYPEMAP = $(GSOAP_DIR)/gsoap/typemap.dat
endif
#MAKE
.PHONY: all
all: wsdl gsoap cp $(client) #$(server)
$(server):$(SERVER_OBJS) $(COM_OBJS)
$(G++) $(INCLS) $(CFLAGS) -o $@ $^ $(LIBS)
$(client):$(CLIENT_OBJS) $(COM_OBJS)
$(G++) $(INCLS) $(CFLAGS) -o $@ $^ $(LIBS)
$(COM_OBJS) $(CLIENT_OBJS) $(SERVER_OBJS):%.o:%.cpp
$(G++) $(INCLS) $(CFLAGS) -o $@ -c $^
wsdl:
ifneq ($(WSDL_FILES), $(wildcard $(WSDL_FILES)))
$(GSOAP_DIR)/gsoap/bin/linux386/wsdl2h -o $(TYPEMAP) $(wildcard *.wsdl) -o $(WSDL_FILES)
endif
gsoap:
ifneq ($(GSOAP_FILES), $(wildcard $(GSOAP_FILES)))
$(GSOAP_DIR)/gsoap/bin/linux386/soapcpp2 -i $(WSDL_FILES) -I $(GSOAP_DIR)/gsoap/import
endif
cp:
ifneq ($(CP_FILES), $(wildcard $(CP_FILES)))
@cp $(GSOAP_DIR)/gsoap/stdsoap2.cpp $(GSOAP_DIR)/gsoap/stdsoap2.h ./ -v
endif
#clean
clean:
@rm -rfv *.o *~ *Proxy.h *.xml *.nsmap *.a *Object.h soapC.* $(WSNAME0)Service.* $(WSNAME0)Proxy.* $(server) $(client) *.nsmap $(CP_FILES) $(GSOAP_FILES)
(3)服务端程序SmsWBSserver.cpp:
#include "soapSmsWBSService.h"
#include "SmsWBS.nsmap"
int main(int argc, char **argv)
{
SmsWBSService sms;
if (argc < 2)
sms.serve(); /* serve as CGI application */
else
{
int port = atoi(argv[1]);
if (!port)
{
fprintf(stderr, "Usage: SmsWBSserver++ <port>\n");
exit(0);
}
/* run iterative server on port until fatal error */
if (sms.run(port))
{
sms.soap_stream_fault(std::cerr);
exit(-1);
}
}
return 0;
}
int SmsWBSService::add(int num1, int num2, int *sum)
{
*sum = num1 + num2;
return SOAP_OK;
}
以上的C和C++版本的Web Service服务端运行后,在本机或者其它机(windows系统也一样)浏览器中输入http://192.168.1.102:9000/SmsWBS?wsdl,会返回XML文件内容,并返回Web Service的功能函数描述,实现了允许客户端通过http的get操作来获取 SmsWBS.wsdl文件。
部分截屏:
(4)客户端程序SmsWBStest.cpp:
#include <stdio.h>
#include <stdlib.h>
#include "soapSmsWBSProxy.h"
#include "SmsWBS.nsmap"
int main(int argc, char **argv)
{
int result = -1;
char* server="http://localhost:9000";
int num1 = 0;
int num2 = 0;
int sum = 0;
if( argc < 3 )
{
printf("usage: %s num1 num2 \n", argv[0]);
exit(0);
}
num1 = atoi(argv[1]);
num2 = atoi(argv[2]);
SmsWBSProxy sms;
result = sms.add(num1, num2, &sum);
if (result != 0)
{
printf("soap err, errcode = %d \n", result);
}
else
{
printf("%d + %d = %d \n", num1, num2, sum);
}
return 0;
}
(5)编译运行,与C版本类似,只是服务端运行时没有提示信息
更多推荐
所有评论(0)