Ubuntu 下的webservices

來源:互聯網
上載者:User

標籤:開頭   namespace   mdi   wsdl   程式猿   connect   手工   tran   har   

搞 了一下午:

開發server程式。需使用gSOAP產生server端代碼架構。

我們有兩種做法:

  1. 編寫WSDL,使用wsdl2h產生標頭檔,再soapcpp2產生架構代碼。
  2. 編寫標頭檔。使用soapcpp2產生架構代碼;

這兩種方式。結果是一樣的。終雩都有產生標頭檔。並產生代碼。不同在於。在項目的開發中須要維護的檔案不同。前者是須要維護WSDL檔案,後者維護標頭檔。

我個人認為另外一種方式更好用,不不過少了個步驟。而是WSDL的文法太難寫了,有點XSD的味道。而標頭檔的編寫。更接近於程式猿的思考方式,比方定義訊息結構,定義介面名稱等。

gSOAP是很智能的,它利用C/C++的凝視來擷取資訊,所以在手工編寫的標頭檔裡,凝視是用用處的。常以// gsoap 名字空間 …開頭。

做為學習。我準備為php blog程式wordpress寫一個web service介面。名字叫wpsoap。


給出代碼:

1 
[email protected]:/home/aries/Aries/gsoap# cat add.h //gsoapopt cw//gsoap ns2 schema namespace: urn:add//gsoap ns2 schema form: unqualified//gsoap ns2 service name: add//gsoap ns2 service type: addPortType//gsoap ns2 service port:http://websrv.cs.fsu.edu/~engelen/addserver.cgi//gsoap ns2 service namespace: urn:add//gsoap ns2 service transport: http://schemas.xmlsoap.org/soap/http//gsoap ns2  service method-style:      add rpc//gsoap ns2  service method-encoding:   //add http://schemas.xmlsoap.org/soap/encoding///gsoap ns2  service method-action:     add ""int ns2__add( int num1, int num2, int* sum );

注意:凝視的的內容也必須加上

2 運行soapcpp2 -c add.h


3 加入一個服務端 addserver.c

[email protected]:/home/aries/Aries/gsoap# cat addserver.c #include "soapH.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, 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);            soap_end(&add_soap);        }    }    return 0;}int ns2__add(struct soap *add_soap, int num1, int num2, int *sum){    *sum = num1 + num2;    return 0;}

4 t加入 客服端 addclient.c

[email protected]:/home/aries/Aries/gsoap# cat addclient.c#include "soapStub.h"#include "add.nsmap"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, namespaces);    soap_call_ns2__add(&add_soap, server, NULL, num1, num2, sum);    printf("server is %s, num1 is %d, num2 is %d/n", server, num1, num2);    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;}

5 測試 上面的addtest.c

[email protected]:/home/aries/Aries/gsoap# cat addtest.c#include <stdio.h>#include <stdlib.h>#include <string.h>int add(const char *server, int num1, int num2, int *sum);int main(int argc, char **argv){    int result = -1;    char server[128] = {0};    int num1;    int num2;    int sum;    if (argc < 4) {        printf("usage: %s <ip:port> num1 num2 /n", argv[0]);        exit(1);    }    strcpy(server,argv[1]);    num1 = atoi(argv[2]);    num2 = atoi(argv[3]);    result = add(server, num1, num2, &sum);    if (result != 0) {        printf("soap error, errcode=%d/n", result);    } else {        printf("%d + %d = %d/n", num1, num2, sum);    }    return 0;}

注意:編譯的時候我們須要gsoap包裡的源碼檔案,把stdsoap2.c和stdsoap2.h檔案複製到當前檔案夾


6 makefile

[email protected]:/home/aries/Aries/gsoap# cat makefileGSOAP_ROOT = /mnt/hgfs/E/gsoap_2.8.17/gsoap-2.8/gsoapWSNAME = addCC = g++ -g -DWITH_NONAMESPACESINCLUDE = -I$(GSOAP_ROOT)SERVER_OBJS = soapC.o stdsoap2.o soapServer.o $(WSNAME)server.o CLIENT_OBJS = soapC.o stdsoap2.o soapClient.o $(WSNAME)client.o $(WSNAME)test.oall: serverserver: $(SERVER_OBJS) $(CC) $(INCLUDE) -o $(WSNAME)server $(SERVER_OBJS) client: $(CLIENT_OBJS) $(CC) $(INCLUDE) -o $(WSNAME)test $(CLIENT_OBJS)cl:rm -f *.o *.xml *.a *.wsdl *.nsmap soapH.h $(WSNAME)Stub.* $(WSNAME)server ns.xsd $(WSNAME)test

[email protected]:/home/aries/Aries/gsoap# makeg++ -g -DWITH_NONAMESPACES    -c -o soapC.o soapC.cg++ -g -DWITH_NONAMESPACES    -c -o stdsoap2.o stdsoap2.cg++ -g -DWITH_NONAMESPACES    -c -o soapServer.o soapServer.cg++ -g -DWITH_NONAMESPACES    -c -o addserver.o addserver.cg++ -g -DWITH_NONAMESPACES -I/mnt/hgfs/E/gsoap_2.8.17/gsoap-2.8/gsoap -o addserver soapC.o stdsoap2.o soapServer.o addserver.o  

繼續 make client


最後 檔案例如以下:

add.add.req.xml  addserver    makefile         soapH.h          stdsoap2.h
add.add.res.xml  addserver.c  soapC.c          soapServer.c     stdsoap2.o
addclient.c      addserver.o  soapClient.c     soapServerLib.c
addclient.o      
addtest      soapClientLib.c  soapServer.o
add.h            addtest.c    soapClient.o     soapStub.h
add.nsmap        addtest.o    soapC.o          stdsoap2.c


效果:



Ubuntu 下的webservices

聯繫我們

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