windows下vs2012用gsoap開發webservice執行個體

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   for   sp   

零:說明

  1、本文是根據網上前人經驗結合自己動手操作寫成,開發工具用的vs2012,gsoap用的是gsoap-2.8;

  2、gsoap提供的工具簡單介紹

    1)wsdl2h.exe:根據WSDL檔案產生標頭檔。這個是別人發布了webservice服務,提供給我們WSDL檔案,我們根據WSDL產生標頭檔,進而實現我們的客     戶端。

    2)soapcpp2.exe:根據標頭檔產生調用遠程soap服務的用戶端架構和提供soap服務的服務端架構。如果我們要自己做server端,則需要自己寫標頭檔,          根據標頭檔,產生用戶端架構和服務端架構,在此架構中實現自己的服務。

一:根據標頭檔用soapcpp2.exe產生架構代碼

  0、該soap服務端是實現兩個整數相加,供遠程調用;

  1、編寫標頭檔

    1)在vs中建立解決方案,名字為gSoapTest

      

    2)在項目gSoapTest下建立標頭檔,名字為add.h

      

      標頭檔代碼:      

//gsoap ns service name: add//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 );

     3)將gsoap安裝目錄下的\gsoap-2.8\gsoap\bin\win32下的soapcpp2.exe拷貝到add.h同一目錄下。

     此處我的目錄是E:\biancheng\gSoapTest\gSoapTest  

     

    4)在控制台中跳轉到add.h目錄下,執行soapcpp2.exe add.h,最後會顯示Compilation successful,代表執行成功

      

    此時E:\biancheng\gSoapTest\gSoapTest該目錄下回產生很多檔案

    

二、根據架構實現server端代碼

  1、在gSoapTest解決方案下建立項目,項目名為server。此時,server所在目錄為E:\biancheng\gSoapTest\server

    1)將E:\biancheng\gSoapTest\gSoapTest下的檔案add.nsmap,add.h,soapH.h,soapStub.h  soapC.cpp,soapServer.cpp以及

     \gsoap-2.8\gsoap\bin\win32目錄下的stdsoap2.h,stdsoap2.cpp拷貝到server項目目錄(E:\biancheng\gSoapTest\server)下。

    2)將add.nsmap,add.h,soapH.h,soapStub.h,stdsoap2.h添加到項目server標頭檔中。

    3)將soapC.cpp,soapServer.cpp,stdsoap2.cpp添加到項目server源檔案中。

    4)在項目server源檔案中建立源檔案,名字為server.cpp

    

    5)將wsock32.lib庫添加到server項目中

    右鍵server->屬性->配置屬性->連接器->輸入->附加依賴項,在附加依賴項中添加wsock32.lib

     

     6)編寫server.cpp代碼   

 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include "stdsoap2.h" 4 #include "add.h" 5 #include "add.nsmap" 6 int main(int argc, char* argv[])  7 { 8     int m, s; 9     struct soap add_soap;10     soap_init(&add_soap);11     //soap_set_namespaces(&add_soap, add_namespaces);12     if (argc < 2) 13     {14         printf("usage: %s <server_port> \n", argv[0]);15         exit(1);16     }17     else18     {19         m = soap_bind(&add_soap, NULL, atoi(argv[1]), 100); 20         if (m < 0)21         {22             soap_print_fault(&add_soap, stderr);23             exit(-1);24         }25         fprintf(stderr, "Socket connection successful: master socket = %d\n", m); 26         for ( ; ; )27         {28             s = soap_accept(&add_soap);29             if (s < 0)30             {31                 soap_print_fault(&add_soap, stderr);32                 exit(-1);33             }34             fprintf(stderr, "Socket connection successful: slave socket = %d\n", s); 35             soap_serve(&add_soap);//該句說明該server的服務36             soap_end(&add_soap); 37         }38     }39     return 0;40 }41 42 //server端的實現函數與add.h中聲明的函數相同,但是多了一個當前的soap串連的參數43 44 int ns__add(struct soap *add_soap, int num1, int num2, int *sum)45 {46     *sum = num1 + num2;47     return 0;48 }
View Code

 二、根據架構實現client端代碼

  1、在解決方案gSoapTest中建立項目,名字為client。此時,server所在目錄為E:\biancheng\gSoapTest\client

    1)將E:\biancheng\gSoapTest\gSoapTest下的檔案add.nsmap,add.h,soapH.h,soapStub.h  soapC.cpp,soapClient.cpp以及

     \gsoap-2.8\gsoap\bin\win32目錄下的stdsoap2.h,stdsoap2.cpp拷貝到client項目目錄(E:\biancheng\gSoapTest\client)下。

    2)將add.nsmap,add.h,soapH.h,soapStub.h,stdsoap2.h添加到項目client標頭檔中。

    3)將soapC.cpp,soapClient.cpp,stdsoap2.cpp添加到項目client源檔案中。

    4)在項目server源檔案中建立源檔案,名字為client.cpp

    

    5)編寫clent.cpp代碼   

#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, &sum);    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);    //該函數是用戶端調用的主要函數,後面幾個參數和add.h中聲明的一樣,前面多了3個參數,函數名是介面函數名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;}
View Code

三、測試

  1、分別編譯server、client

  

  2、啟動server,並綁定訪問連接埠號碼4567

  

  通過IE訪問http://localhost:4567,如下,則服務端成功

  

  3、啟動client,輸入1 2,如下,代表用戶端訪問服務端成功

  

  

   

windows下vs2012用gsoap開發webservice執行個體

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.