|
gSOAP是一個夸平臺的,用于開發(fā)Web Service服務(wù)端和客戶端的工具,在Windows、Linux、MAC OS和UNIX下使用C和C++語言編碼,集成了SSL功能。
官方網(wǎng)站:http:///Products/gsoap/index.html
對于Windows平臺下開發(fā)客戶端,首先下載最新的gsoap_win32_2.7.6c.zip包,具體在以下地址:http://optusnet.dl./sourceforge/gsoap2 /gsoap_win32_2.7.6c.zip
首先查看gsoap的User's Guide,基本就能對gsoap有個全面的了解,通過閱讀Sample里的例子程序深入。然后搜索網(wǎng)上其它一些文章,比如:
gSOAP簡單多線程服務(wù)器程序:http://blog./u1/55091/showart_430965.html 純c的gSoap實現(xiàn)WebService:http://hi.baidu.com/2sky2sea/blog/item/40ec5555680279c1b745ae9b.html
我以網(wǎng)上出現(xiàn)的實現(xiàn)一個簡單的加法函數(shù)為例,講講我在操作過程中遇到的問題。
一、服務(wù)器端
1、首先編寫 add.h文件: //gsoap ns service namespace: http://localhost/add.wsdl //gsoap ns service location: http://localhost //gsoap ns service executable: add.cgi //gsoap ns service encoding: encoded //gsoap ns schema namespace: urn:add
int ns__add( int num1, int num2, int* sum );
2、用gsoap/bin目錄下的soapcpp2.exe程序,生成一些文件。 可以把soapcpp2.exe拷貝到一add.h目錄下,用cmd執(zhí)行soapcpp2.exe add.h。 在這個目錄下會自動生成許多將來有用的文件,如add.namap,soapH.h,soapC.cpp,soapClient.cpp,soapServer.cpp等文件。 soapcpp2.exe可以帶參數(shù)執(zhí)行,具體執(zhí)行soapcpp2.exe -h查看。
3、新建一個win32控制臺工程,加入wsock32.lib庫,將剛才生成的那些文件添加到工程中。
然后編寫webserver.cpp主程序: #include <stdio.h> #include <stdlib.h> #include "stdsoap2.h" #include "add.h" #include "add.nsmap" int main(int argc, char* argv[]) { int m, s; struct soap add_soap; soap_init(&add_soap); //soap_set_namespaces(&add_soap, add_namespaces); if (argc < 2) { printf("usage: %s <server_port> \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 connection successful: slave socket = %d\n", s); soap_serve(&add_soap);//該句說明該server的服務(wù) soap_end(&add_soap); } } return 0; }
//server端的實現(xiàn)函數(shù)與add.h中聲明的函數(shù)相同,但是多了一個當前的soap連接的參數(shù)
int ns__add(struct soap *add_soap, int num1, int num2, int *sum) { *sum = num1 + num2; return 0; }
4、編譯 編譯這個程序,會提示錯誤,將gsoap_win32目錄下stdsoap2.cpp,stdsoap2.h文件加入工程,重新編譯如果還有錯誤,可能是你將add.h生成的文件添加入工程出錯的原因。 實際上在編寫server程序時,無須帶Client的那些文件,還有帶Lib的文件也無須添加到工程中。 調(diào)試成功之后,在cmd中執(zhí)行gsoap.exe文件,并設(shè)定第一個參數(shù)為4567。在IE中輸入http://localhost:4567,如果顯示xml頁面,說明程序已經(jīng)啟動。
二、客戶端
1、 客戶端程序代碼如下:\
#include <stdio.h> #include <stdlib.h> #include "stdsoap2.h" #include "soapH.h" #include "add.nsmap"
int add(const char* server, int num1, int num2, int *sum);
int main(int argc, char **argv) { int result = -1; char* server="http://localhost:4567"; 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]); result = add(server, num1, num2, ∑); if (result != 0) { printf("soap err,errcode = %d\n", result); } else { printf("%d+%d=%d\n", num1, num2, sum ); } return 0; }
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, add_namespaces); //該函數(shù)是客戶端調(diào)用的主要函數(shù),后面幾個參數(shù)和add.h中聲明的一樣,前面多了3個參數(shù),函數(shù)名是接口函數(shù)名ns__add前面加上soap_call_ soap_call_ns__add( &add_soap, server, "", num1, num2, sum ); 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; }
2、客戶端程序既可以新建一個新的win32控制臺程序,將剛才生成的nsmap,soapH.h,soapClient.h等文件加入工程,編譯既可。需要注意,必須配置Lib文件
3、服務(wù)器與客戶端的連調(diào)測試 (1)啟動服務(wù)器端 gsoap.exe 4567 (2)啟動客戶端 gsoap_client.exe 1 2 顯示的結(jié)果為:1+2=3 |
|
|