1. gsoap的好處就不用說了:百度百科
2. gsoap的下載地址:項目地址,目前我使用的是2.8.15版本
3. 開發環境:Ubuntu13.10
4. 具體操作步驟(以簡單相加為例):
1)編寫add.h(頭文件)
//gsoap ns service name: calc //gsoap ns service protocol: SOAP //gsoap ns service style: rpc //gsoap ns service encoding: encoded //gsoap ns service namespace: http://localhost:8888 //gsoap ns service location: http://localhost:8888 //gsoap ns service port: http://localhost:8888 int ns__add( int num1, int num2, int* sum );
2)編寫addserver.cpp(服務器)
#include "soapcalcService.h" /* 與add.h中第一行ns有關 */ #include "calc.nsmap" /* 與add.h中第一行ns有關 */ int main(int argc, char **argv) { calcService calc; /* 創建calc對象 */ if (argc < 2) calc.serve(); /* serve as CGI application */ else { int port = atoi(argv[1]); if (!port) { fprintf(stderr, "Usage: server <port>\n"); exit(0); } /* run iterative server on port until fatal error */ if (calc.run(port)) /* 運行在port端口上 */ { calc.soap_stream_fault(std::cerr); exit(-1); } } return 0; } int calcService::add(int a, int b, int *result) /* 在此處實現add() */ { *result = a + b; return SOAP_OK; }
3)編寫addclient.cpp(客戶端)
#include "soapcalcProxy.h" /* 與add.h第一行的ns有關 */ #include "calc.nsmap" /* 與add.h第一行的ns有關 */ #include <stdio.h> #include <stdlib.h> const char server[] = "127.0.0.1:4567"; int main(int argc, char **argv) { if (argc < 3) { fprintf(stderr, "Usage: client num1 num2\n"); exit(0); } int a, b, result; a = (int)(strtod(argv[1], NULL)); /* str轉double再轉int */ b = (int)(strtod(argv[2], NULL)); calcProxy calc; /* 創建calc對象 */ calc.soap_endpoint = server; /* 設定server */ calc.add(a, b, &result); /* 執行add() */ if (calc.error) calc.soap_stream_fault(std::cerr); else printf("result = %d\n", result); /* 打印消息 */ return 0; }
4)編寫Makefile文件
# this is a Makefile to build client and server # please setting the GSOAP_ROOT first. # build procedure(EXAMPLE): server run on pc, then client run on arm # step1: setting GSOAP_ROOT # step2: setting OBJ_NS # the first line in add.h # step3: setting OBJ_NAME # the basename of filename of add.h # step4: setting CC and CXX # step4: make OBJ_NS := calc OBJ_NAME := add GSOAP_ROOT := /home/scue/work/gsoap_2.8.15/gsoap INCLUDE := -I$(GSOAP_ROOT) CC := clang++ GCC := clang++ #CC := arm-linux-g++ #CXX := arm-linux-g++ CFLAGS += -w CXXFLAGS += -w OBJ_SERVER := soapC.o stdsoap2.o soap$(OBJ_NS)Service.o $(OBJ_NAME)server.o OBJ_CLIENT := soapC.o stdsoap2.o soap$(OBJ_NS)Proxy.o $(OBJ_NAME)client.o all: @make soap @make server @make client server: $(OBJ_SERVER) $(CXX) $(CXXFLAGS) $(INCLUDE) $^ -o $@ client: $(OBJ_CLIENT) $(CXX) $(CXXFLAGS) $(INCLUDE) $^ -o $@ .PHONY:soap soap: @cp -v $(GSOAP_ROOT)/stdsoap2.* . @$(GSOAP_ROOT)/bin/linux386/soapcpp2 -i $(OBJ_NAME).h soapqt: @mkdir -p $(OBJ_NS)qtclient @cp -v soapH.h soapStub.h stdsoap2.h soap$(OBJ_NS)Proxy.h \ soapC.cpp stdsoap2.cpp soap$(OBJ_NS)Proxy.cpp $(OBJ_NS).nsmap \ $(OBJ_NS)qtclient/ # -c 生成C的文件 # -i 生成C++的文件 .PHONY:clean clean: rm -f server client *.o distclean: rm -f server client *.o ns* soap* *.xml *.nsmap *.wsdl stdsoap2.*
5)編譯及試驗
make ./server 4567 ./client 2 3 #將會返回5,也可以直接在瀏覽器中輸入http://localhost:4567進行驗證
總結:使用C++編寫將會輕松很多。