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創建webservice服務

下載gsop

首先到http://sourceforge.net/projects/gsoap2/files/gSOAP/下載gsoap的最新版本。解壓后目錄結構如下:
 
我們看到bin\win32目錄下有兩個exe可執行文件:soapcpp2.exe,wsdl2h.exe。另外根目錄還有兩個比較重要的源文件:stdsoap2.h和stdsoap2.cpp。

准備待導出的服務接口定義文件(比如gservice.h)

//gsoap ns service name: gservice
//gsoap ns service style: rpc


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);


新建webservice服務器端(gServer)

選擇空控制台程序,並將soapcpp2.exe、stdsoap2.h、stdsoap2.cpp、gservice.h文件拷貝到gserver目下。啟用cmd命令行程序,進入到gServer目錄下執行如下命令:
soapcpp2.exe -S gservice.h
生成服務器端接口文件。並將stdsoap2.hsoapStub.hsoapH.hgservice.nsmap和stdsoap2.cppsoapC.cppsoapServer.cpp文件導入到gServer中,項目目錄結構如下:
實現main函數:
#include "stdio.h"
#include "soapH.h"
#include "gservice.nsmap"

int main(int argc,char **argv)
{
int nPort = 8080;
struct soap fun_soap;
soap_init(&fun_soap);
int nMaster = (int)soap_bind(&fun_soap, NULL, nPort, 100);
if (nMaster < 0)
{
soap_print_fault(&fun_soap, stderr);
exit(-1);
}

fprintf(stderr, "Socket connection successful : master socket = %d\n", nMaster);

while (true)
{
int nSlave = (int)soap_accept(&fun_soap);
if (nSlave < 0)
{
soap_print_fault(&fun_soap, stderr);
exit(-1);
}


fprintf(stderr, "Socket connection successful : slave socket = %d\n", nSlave);


soap_serve(&fun_soap);
soap_end(&fun_soap);
}


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;
}
編譯,運行服務器端程序,從瀏覽器訪問localhost:8080 如能看到如下信息,說明服務器運行正常:
XML Source Code
 
          

新建webservice客戶端(gClient)

選擇空控制台程序,並將soapcpp2.exe、stdsoap2.h、stdsoap2.cpp、gservice.h文件拷貝到gClient目下。啟用cmd命令行程序,進入到gClient目錄下執行如下命令:
soapcpp2.exe -C gservice.h
生成客戶端代理接口文件。並將stdsoap2.hsoapStub.hsoapH.hgservice.nsmap和stdsoap2.cppsoapC.cppsoapClient.cpp文件導入到gClient中,項目目錄結構如下:


實現main函數:
#include "stdio.h"
#include "gservice.nsmap"

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://localhost:8080";


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;
}

測試服務

啟動服務器端gServer程序:

啟動客戶端gClient程序:



 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM