linux下gsoap的初次使用

來源:互聯網
上載者:User

這兩天,接到一個項目,需要在linux程式中調用遠端web應用,通過soap協議。開始上網查了下資料,發現了gsoap庫這個好東東^_^。繼續在網上搜尋例子代碼,發現基本都不可編譯通過,於是便一邊學習一邊寫了這個最簡單的例子,希望對後來者起到一點協助。

  • 對gsoap的簡單介紹,請自己參閱http://gsoap2.sourceforge.net/

    下載相應的包,主要有2個工具和原始碼:
    wsdl2h -o outfile.h infile.wsdl 實現wsdl檔案到h檔案的資料對應
       
soapcpp2 -c outfile.h產生相應的底層通訊stub,strech程式

  • 下面這個簡單的例子實現的是在用戶端輸入2個數字,然後遠程調用服務端的加法函數,最後返回結果給用戶端。

    在這裡我們不需要wsdl的檔案,可以直接從.h檔案來產生代碼。我們定義一個函式宣告檔案,用來定義介面函數,名稱為add.h,內容如下:

  1. //gsoapopt cw
  2. //gsoap ns2 schema namespace: urn:add
  3. //gsoap ns2 schema form: unqualified
  4. //gsoap ns2 service name: add
  5. //gsoap ns2 service type: addPortType
  6. //gsoap ns2 service port:http://websrv.cs.fsu.edu/~engelen/addserver.cgi
  7. //gsoap ns2 service namespace: urn:add
  8. //gsoap ns2 service transport: http://schemas.xmlsoap.org/soap/http
  9. //gsoap ns2  service method-style:      add rpc
  10. //gsoap ns2  service method-encoding:   
  11. add http://schemas.xmlsoap.org/soap/encoding/
  12. //gsoap ns2  service method-action:     add ""
  13. int ns2__add( int num1, int num2, int* sum );

然後我們執行soapcpp2 -c add.h,自動產生一些遠程調用需要的檔案。

接下來我們寫一個服務端,建立檔案addserver.c

  1. #include "soapH.h"
  2. #include "add.nsmap"
  3. int main(int argc, char **argv)
  4. {
  5.     int m, s;
  6.     struct soap add_soap;
  7.     soap_init(&add_soap);
  8.     soap_set_namespaces(&add_soap, namespaces);
  9.     if (argc < 2) {
  10.         printf("usage: %s <server_port> /n", argv[0]);
  11.         exit(1);
  12.     } else {
  13.         m = soap_bind(&add_soap, NULL, atoi(argv[1]), 100);
  14.         if (m < 0) {
  15.             soap_print_fault(&add_soap, stderr);
  16.             exit(-1);
  17.         }
  18.         fprintf(stderr, "Socket connection successful: master socket = %d/n", m);
  19.         for (;;) {
  20.             s = soap_accept(&add_soap);
  21.             if (s < 0) {
  22.                 soap_print_fault(&add_soap, stderr);
  23.                 exit(-1);
  24.             }
  25.             fprintf(stderr, "Socket connection successful: slave socket = %d/n", s);
  26.             soap_serve(&add_soap);
  27.             soap_end(&add_soap);
  28.         }
  29.     }
  30.     return 0;
  31. }
  32. int ns2__add(struct soap *add_soap, int num1, int num2, int *sum)
  33. {
  34.     *sum = num1 + num2;
  35.     return 0;
  36. }

我們接著寫用戶端,檔案addclient.c

  1. #include "soapStub.h"
  2. #include "add.nsmap"
  3. int add(const char *server, int num1, int num2, int *sum)
  4. {
  5.     struct soap add_soap;
  6.     int result = 0;
  7.     soap_init(&add_soap);
  8.     soap_set_namespaces(&add_soap, namespaces);
  9.     soap_call_ns2__add(&add_soap, server, NULL, num1, num2, sum);
  10.     printf("server is %s, num1 is %d, num2 is %d/n", server, num1, num2);
  11.     if (add_soap.error) {
  12.         printf("soap error: %d, %s, %s/n", add_soap.error, *soap_faultcode(&add_soap), *soap_faultstring(&add_soap));
  13.         result = add_soap.error;
  14.     }
  15.     soap_end(&add_soap);
  16.     soap_done(&add_soap);
  17.     return result;
  18. }

最後寫一個測試代碼,addtest.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int add(const char *server, int num1, int num2, int *sum);
  5. int main(int argc, char **argv)
  6. {
  7.     int result = -1;
  8.     char server[128] = {0};
  9.     int num1;
  10.     int num2;
  11.     int sum;
  12.     if (argc < 4) {
  13.         printf("usage: %s <ip:port> num1 num2 /n", argv[0]);
  14.         exit(1);
  15.     }
  16.     strcpy(server,argv[1]);
  17.     num1 = atoi(argv[2]);
  18.     num2 = atoi(argv[3]);
  19.     result = add(server, num1, num2, ∑);
  20.     if (result != 0) {
  21.         printf("soap error, errcode=%d/n", result);
  22.     } else {
  23.         printf("%d + %d = %d/n", num1, num2, sum);
  24.     }
  25.     return 0;
  26. }

到此為止,我們自己的代碼已經編寫完畢,現在我們來編譯服務端和用戶端
注意:編譯的時候我們需要gsoap包裡的原始碼檔案,把stdsoap2.c和stdsoap2.h檔案拷貝到目前的目錄

我們寫一個Makefile檔案:

  1. GSOAP_ROOT = /root/gsoap-2.7/gsoap
  2. WSNAME = add
  3. CC = g++ -g -DWITH_NONAMESPACES
  4. INCLUDE = -I$(GSOAP_ROOT)
  5. SERVER_OBJS = soapC.o stdsoap2.o soapServer.o $(WSNAME)server.o 
  6. CLIENT_OBJS = soapC.o stdsoap2.o soapClient.o $(WSNAME)client.o $(WSNAME)test.o
  7. all: server
  8. server: $(SERVER_OBJS) 
  9.     $(CC) $(INCLUDE) -o $(WSNAME)server $(SERVER_OBJS) 
  10. client: $(CLIENT_OBJS) 
  11.     $(CC) $(INCLUDE) -o $(WSNAME)test $(CLIENT_OBJS)
  12. cl:
  13.     rm -f *.o *.xml *.a *.wsdl *.nsmap soapH.h $(WSNAME)Stub.* $(WSNAME)server ns.xsd $(WSNAME)test

然後我們執行make,即可生產addserver程式;make client,產生addtest程式。
讓server跑起來,執行./addserver 6666

終端列印出“Socket connection successful: master socket = 3”,那麼你的server已經在前台run起來了;
運行用戶端,./addtest ip:port num1 num2,返回加法的結果。

OK,一個最簡單的soap調用的例子完成了,進深一步的學習請參考http://gsoap2.sourceforge.net/

from:http://blog.csdn.net/jinpw/article/details/3346844

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.