gsoap搭建WebService服务
WebService、soap、gsoap基本概念
WebService服務基本概念:就是一個應用程序,它向外界暴露出一個可以通過web進行調用的API,是分布式的服務組件。本質上就是要以標準的形式實現企業內外各個不同服務系統之間的互調和集成。
soap概念:簡單對象訪問協議,是一種輕量的、簡單的、基于 XML 的協議,它被設計成在 WEB 上交換結構化的和固化的信息。
從這里的概念可以看得出來,soap是一個基于xml格式的web交互協議,而webservice是一種使用web方式實現的功能。就好像是網絡視頻服務器和http的關系,就是這里的webservice服務器和soap的關系。其實從歷史上來說,先有的soap這種協議,然后微軟用基于這種協議制作了webservice這種服務。
gsoap概念:是一種能夠把C/C++語言的接口轉換成基于soap協議的webservice服務的工具。
?
gsoap使用方法
步驟1:首先下載gsoap的工具。這里下載了gsoap_2.7.10.解壓之后,在里面會發現兩個exe可執行文件。soapcpp2.exe和wsdl2h.exe。另外還有兩個比較重要的源文件:stdsoap2.h和stdsoap2.cpp。為了使用方便,可以把兩個exe可執行文件所在的問價設置到環境變量中。
步驟2:新建一個.h文件用來聲明所要暴露給外界的接口。這里以一些運算的接口來作為例子。頭文件命名為calculator.h。注意雖然是.h文件,但是千萬不能有注釋。內容如下:
//gsoap ns service name: calculate
//gsoap ns service style: rpc
//gsoap ns service namespace: http://10.64.57.10/calculate.wsdl
//gsoap ns service location: http://10.64.57.10/calculate.cgi
//gsoap ns schema namespace: urn:calculate
int ns__add(int num1, int num2, int* result );
int ns__sub(int num1, int num2, int* result );
int ns__mult( int num1, int num2, int *result);
int ns__divid( int num1, int num2, int *result);
????步驟3:使用soapcpp2.exe工具來生成相關的源文件。使用方法是soapcpp2 calculator.h
這里沒有帶選項,那么生成的是最全的源文件,包括客戶端的和服務器的以及其它一些文件。當然也可以使用選項來生成需要的文件,可以再命令行下面使用soapcpp2 –help來參看選項。
另外在gsoap的安裝目錄下面有兩個重要的源文件:stdsoap2.h和stdsoap2.cpp。在編譯工程的時候要使用。這里羅列一下生成的重要源文件(包括stdsoap2.h和stdsoap2.cpp),它們是:stdsoap2.h、soapStub.h、soapH.h、calculate.nsmap以及stdsoap2.cpp、soapC.cpp、soapClient.cpp soapServer.cpp。
stdsoap2.h和stdsoap2.cpp是gsoap最底層的實現,它們和每次編寫的頭文件(calculate.h)無關。soapStub.h(存根頭文件),包含了stdsoap2.h,里面聲明了定義在calculate.h中的接口,包括客戶端的和服務器的。還有一個最上層的soapH.h包含了soapStub.h。它是具體實現的聲明。和soapC.cpp對應。calculate.nsmap這個文件應該是soap協議相關的一個東西。在編程序時include “calculate.nsmap”就可以了。再來介紹一下源文件,stdsoap2.cpp是和stdsoap2.h對應的,實現最底層的運作,和calculate.h無關;soapC.cpp是和soapH.h對應的,是接口相關的。但是它提供的僅僅是服務器和客戶端共有部分的實現。soapClient.cpp也是接口相關的,它里面實現的僅僅是客戶端需要的代碼。同理soapServer.cpp也是只和服務器相關的。
???????? 好了介紹完了這些源文件,再來梳理一遍。工程中需要的頭文件有stdsoap2.h、soapStub.h、soapH.h、calculate.nsmap。但是因為soapStub.h包含了stdsoap2.h;soapH.h包含了soapStub.h,所以工程中只需要包含soapH.h、calculate.nsmap。對于源文件,若是服務器端,需要stdsoap2.cpp、soapC.cpp和soapServer.cpp;客戶端則需要stdsoap2.cpp、soapC.cpp和soapClient.cpp;
???????? 為了偷懶,編寫了一個腳本來實現調用soapcpp2和拷貝stdsoap2.h、stdsoap2.cpp的功能。名字為AutoGsoap.bat。使用方法是把它放到你定義的calculate.h目錄下,然后運行,輸入你要操作的頭文件名字即可。腳本內容如下:
@echo off
echo "請輸入頭文件"
set /p headfile=
soapcpp2.exe %headfile%
copy D:\MyApp\ProfessionalApp\gsoap_2.7.10\gsoap-2.7\gsoap\stdsoap2.h? .\?
copy D:\MyApp\ProfessionalApp\gsoap_2.7.10\gsoap-2.7\gsoap\stdsoap2.cpp? .\
pause
?
???????? 步驟4:建立服務器端的工程。這里以CalculateServer為例?。新建工程后,添加“wsock32.lib”庫,gsoap需要用到sock套接字通信。
?
?
然后編寫服務器端的主函數,名稱為main.cpp,內容如下:
?
#include "soapH.h"
#include "calculate.nsmap"
#include "stdio.h"
int main( int argc, char *argv[])
{
??? struct soap *CalculateSoap = soap_new();??????????????????????????????? //創建一個soap
??? int iSocket_master = soap_bind(CalculateSoap, NULL, 10000, 100);???????? //綁定到相應的IP地址和端口()NULL指本機,
??????????????????????????????????????????????????????????????????????????? //10000為端口號,最后一個參數不重要。
??? if (iSocket_master< 0)????????????????????????????????????????????????? //綁定出錯
??? {
??????? soap_print_fault(CalculateSoap, stderr);
??????? exit(-1);
??? }
??? printf("SoapBind success,the master socket number is:%d\n",iSocket_master); //綁定成功返回監聽套接字
???
??? while(1)
??? {
??????? int iSocket_slaver = soap_accept(CalculateSoap);
??????? if (iSocket_slaver < 0)
??????? {
??????????? soap_print_fault(CalculateSoap, stderr);
??????????? exit(-2);
??????? }
??????? printf("Get a new connection,the slaver socket number is:%d\n",iSocket_slaver); //綁定成功返回監聽套接字
??????? soap_serve(CalculateSoap);
??????? soap_end(CalculateSoap);
??? }
??? soap_done(CalculateSoap);
??? free(CalculateSoap);
??? return 0;
}
/*加法的具體實現*/
int ns__add(struct soap *soap, int num1, int num2, int* result )??
{
??? if (NULL == result)
??? {
??????? printf("Error:The third argument should not be NULL!\n");
??????? return SOAP_ERR;
??? }
??? else
??? {
??????? (*result) = num1 + num2;
??????? return SOAP_OK;
??? }
??? return SOAP_OK;
}
/*減法的具體實現*/
int ns__sub(struct soap *soap,int num1, int num2, int* result )
{
??? if (NULL == result)
??? {
??????? printf("Error:The third argument should not be NULL!\n");
??????? return SOAP_ERR;
??? }
??? else
??? {
??????? (*result) = num1 - num2;
??????? return SOAP_OK;
??? }
??? return SOAP_OK;
}
/*乘法的具體實現*/
int ns__mult(struct soap *soap, int num1, int num2, int *result)
{
??? if (NULL == result)
??? {
??????? printf("Error:The third argument should not be NULL!\n");
??????? return SOAP_ERR;
??? }
??? else
??? {
??????? (*result) = num1 * num2;
??????? return SOAP_OK;
??? }
??? return SOAP_OK;
}
/*除法的具體實現*/
int ns__divid(struct soap *soap, int num1, int num2, int *result)
{
??? if (NULL == result || 0 == num2)
??? {
??????? printf("Error:The second argument is 0 or The third argument is NULL!\n");
??????? return SOAP_ERR;
??? }
??? else
??? {
??????? (*result) = num1 / num2;
??????? return SOAP_OK;
??? }
??? return SOAP_OK;
}
?
編譯,運行服務器端程序,從IE瀏覽器訪問10.64.57.10:10000 (該服務器運行的本機IP地址為10.64.57.10)如能看到如下信息,說明服務器運行正常:
?
???????? 步驟5:新建客戶端程序進行驗證。新建工程如下,同樣要包含wsock32.lib。
?
/*客戶端主程序*/
#include "soapH.h"
#include "calculate.nsmap"
#include "stdio.h"
int main( int argc, char *argv[])
{
??? printf("The Client is runing...\n");
??? int num1 = 110;
??? int num2 = 11;
??? int result = 0;
??? struct soap *CalculateSoap = soap_new();
??? //soap_init(CalculateSoap);
??? char * server_addr = "http://10.64.57.10:10000";
??? int iRet = soap_call_ns__add(CalculateSoap,server_addr,"",num1,num2,&result);
??? if ( iRet == SOAP_ERR)
??? {
??????? printf("Error while calling the soap_call_ns__add");
??? }
??? else
??? {
??????? printf("Calling the soap_call_ns__add success。\n");
??????? printf("%d + %d = %d\n",num1,num2,result);
??? }
??? iRet = soap_call_ns__sub(CalculateSoap,server_addr,"",num1,num2,&result);
??? if ( iRet == SOAP_ERR)
??? {
??????? printf("Error while calling the soap_call_ns__sub");
??? }
??? else
??? {
??????? printf("Calling the soap_call_ns__sub success。\n");
??????? printf("%d - %d = %d\n",num1,num2,result);
??? }
??? iRet = soap_call_ns__mult(CalculateSoap,server_addr,"",num1,num2,&result);
??? if ( iRet == SOAP_ERR)
??? {
??????? printf("Error while calling the soap_call_ns__mult");
??? }
??? else
??? {
??????? printf("Calling the soap_call_ns__mult success。\n");
??????? printf("%d * %d = %d\n",num1,num2,result);
??? }
??? iRet = soap_call_ns__divid(CalculateSoap,server_addr,"",num1,num2,&result);
??? if ( iRet == SOAP_ERR)
??? {
??????? printf("Error while calling the soap_call_ns__divid");
??? }
??? else
??? {
??????? printf("Calling the soap_call_ns__divid success。\n");
??????? printf("%d / %d = %d\n",num1,num2,result);
??? }
??? soap_end(CalculateSoap);
??? soap_done(CalculateSoap);
??? free(CalculateSoap);
??? return 0;
}
?
?
?
?
開啟服務器之后,運行客戶端程序,打印信息如下:
?
此時服務器端也檢測到了四個連接:
?
總結
以上是生活随笔為你收集整理的gsoap搭建WebService服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 世界征服者4剧本攻略是什么
- 下一篇: 短信远程开机