WebServices under Ubuntu

Source: Internet
Author: User
Tags wsdl

After an afternoon of doing:

Develop the server program. The server-side code framework needs to be generated using gSOAP.

We have two ways to do this:

    1. Write the WSDL, use wsdl2h to generate the header file, and then soapcpp2 generate the skeleton code.
    2. Write the header file. Generate framework code using SOAPCPP2;

Both of these ways. The result is the same. Finally, there is a file that produces a header. and generate the code. The difference is. The files that need to be maintained in the development of the project are different. The former is required to maintain the WSDL file, which maintains the header file.

I personally think that the other way is better to use, not just a few steps. But the WSDL syntax is too hard to write, a bit of an XSD flavor. And the writing of the header file. Closer to the way the program apes think, such as defining message structures, defining interface names, and so on.

gSOAP is very intelligent, it uses the C + + gaze to get information, so in the hand-written header file, staring is useful. Often with//gSOAP namespaces ... Beginning.

As a study. I am going to write a Web service interface for the PHP blog program WordPress. The name is Wpsoap.


Give the code:

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

Note: The content of the gaze must also be added

2Run soapcpp2-c add.h


3 Join a server 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 (&AMP;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 (&AMP;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 (&AMP;ADD_SOAP);        Soap_end (&AMP;ADD_SOAP); }} return 0;} int Ns2__add (struct sOAP *add_soap, int num1, int num2, int *sum) {*sum = Num1 + num2; return 0;}

4 T join the customer service terminal 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 are%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 Test the above 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;}

Note: When compiling, we need to gsoap the source files in the package, and copy the stdsoap2.c and stdsoap2.h files to the current folder .


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  

Continue make Client


The final file is as follows:

add.add.re Q.xml   addserver     Makefile         soapH.h          stdsoap2.h
Add.add.res.xml  addser VER.C  soapc.c         &NBSP;SOAPSERVER.C     STDSOAP2.O
addclient.c     & NBSP;ADDSERVER.O  soapclient.c     SOAPSERVERLIB.C
ADDCLIENT.O       addtest      soapclientlib.c   SOAPSERVER.O
Add.h            addtest.c   &NBSP;SOAPCLIENT.O     Soapstub . h
Add.nsmap       &NBSP;ADDTEST.O   &NBSP;SOAPC.O          stdsoap2.c


Effect:



WebServices under Ubuntu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.