Windows vs2012 using gSOAP to develop WebService instances

Source: Internet
Author: User

0: Description

1, this article is based on the experience of the online predecessors combined with their own hands-on writing, development tools used Vs2012,gsoap is gsoap-2.8;

2, gSOAP provides a simple introduction to the tools

1) Wsdl2h.exe: Generates the header file from the WSDL file. This is someone else released the WebService service, provided to us WSDL file, we generate header file according to WSDL, and then implement our client.

2) Soapcpp2.exe: Generates the client framework that invokes the remote SOAP service and the service-side framework that provides the SOAP service, based on the header file . If we want to do the server side, we need to write our own header files, based on the header file, build the client framework and the service-side framework, in this framework to implement their own services.

One: Generate framework code with Soapcpp2.exe based on header file

0, the SOAP service side is to implement two integers added for remote invocation;

1. Writing header files

1) New solution in VS, named Gsoaptest

      

2) Create a new header file under Project Gsoaptest with the name Add.h

      

Header File Code:      

gSOAP NS service name:add//gsoap NS service namespace:http://localhost/add.wsdl//gsoap NS service Location:http://loc ALHOST//GSOAP NS service executable:add.cgi//gsoap NS service encoding:encoded//gsoap NS schema Namespace:urn:add int n S__add (int num1, int num2, int* sum);

3) Copy the Soapcpp2.exe under \gsoap-2.8\gsoap\bin\win32 under the gSOAP installation directory to the Add.h same directory.

My directory here is E:\biancheng\gSoapTest\gSoapTest.

4) Jump to the Add.h directory in the console, execute Soapcpp2.exe add.h, and the compilation successful will be displayed, on behalf of the successful execution

A lot of files are generated next time E:\biancheng\gSoapTest\gSoapTest this directory

    

Implementation of server-side code according to the framework

1. Create a new project under the Gsoaptest solution, the project name is server. At this point, the directory where the server is E:\biancheng\gSoapTest\server

1) E:\biancheng\gSoapTest\gSoapTest the file under Add.nsmap,add.h,soaph.h,soapstub.h soapc.cpp,soapserver.cpp and

The stdsoap2.h,stdsoap2.cpp copy under the \gsoap-2.8\gsoap\bin\win32 directory to the Server project directory (E:\biancheng\gSoapTest\server).

2) Add add.nsmap,add.h,soaph.h,soapstub.h,stdsoap2.h to the Project Server header file.

3) Add Soapc.cpp,soapserver.cpp,stdsoap2.cpp to the Project Server source file.

4) Create a new source file in the Project Server source file named Server.cpp

    

5) Add the Wsock32.lib library to the server project

Right-server-> Properties--Configuration Properties--Additional dependencies, input, connector--add Wsock32.lib in additional dependencies

6) Write Server.cpp code

1#include <stdio.h>2#include <stdlib.h>3#include"stdsoap2.h"4#include"add.h"5#include"Add.nsmap"6 intMainintargcChar*argv[])7 {8     intm, S;9     structsoap Add_soap;TenSoap_init (&add_soap); One     //soap_set_namespaces (&add_soap, add_namespaces); A     if(ARGC <2)  -     { -printf"usage:%s <server_port> \ n", argv[0]); theExit1); -     } -     Else -     { +m = Soap_bind (&add_soap, NULL, Atoi (argv[1]), -);  -         if(M <0) +         { ASoap_print_fault (&Add_soap, stderr); atExit (-1); -         } -fprintf (stderr,"socket connection Successful:master socket =%d\n", M);  -          for ( ; ; ) -         { -s = soap_accept (&add_soap); in             if(S <0) -             { toSoap_print_fault (&Add_soap, stderr); +Exit (-1); -             } thefprintf (stderr,"socket connection Successful:slave socket =%d\n", s);  *Soap_serve (&AMP;ADD_SOAP);//This sentence describes the server's service $Soap_end (&add_soap); Panax Notoginseng         } -     } the     return 0; + } A  the //The server-side implementation function is the same as the function declared in Add.h, but one more parameter of the current SOAP connection +  - intNs__add (structSoap *add_soap,intNUM1,intNUM2,int*sum) $ { $*sum = Num1 +num2; -     return 0; -}
View Code

Second, the client-side code is implemented according to the framework

1, the new project in the solution Gsoaptest, the name is client. At this point, the directory where the server is E:\biancheng\gSoapTest\client

1) E:\biancheng\gSoapTest\gSoapTest the file under Add.nsmap,add.h,soaph.h,soapstub.h soapc.cpp,soapclient.cpp and

Stdsoap2.h,stdsoap2.cpp copy under the \gsoap-2.8\gsoap\bin\win32 directory to the Client project directory (E:\biancheng\gSoapTest\client).

2) Add add.nsmap,add.h,soaph.h,soapstub.h,stdsoap2.h to the project client header file.

3) Add Soapc.cpp,soapclient.cpp,stdsoap2.cpp to the project client source file.

4) Create a new source file in the Project Server source file named Client.cpp

    

5) Write Clent.cpp code

#include <stdio.h>#include<stdlib.h>#include"stdsoap2.h"#include"soapH.h"#include"Add.nsmap"intAddConst Char* Server,intNUM1,intNUM2,int*sum); intMainintargcChar**argv) {    intresult =-1; Char* server="http://localhost:4567"; intNUM1 =0; intnum2 =0; intsum =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;}intAddConst Char* Server,intNUM1,intNUM2,int*sum) {    structsoap Add_soap; intresult =0; Soap_init (&add_soap); //soap_set_namespaces (&add_soap, add_namespaces); //The function is the main function of the client call, the following parameters are declared in the Add.h, the previous 3 parameters, the function name is the interface functions of the name Ns__add preceded by the//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); returnresult;}
View Code

Third, testing

1. Compile server, client separately

  

2. Start server, and bind access port number 4567

  

Access to http://localhost:4567 via IE, as below, then the server succeeds

  

3, start the client, enter 1 2, as below, on behalf of clients to access the server successfully

  

  

   

Windows vs2012 using gSOAP to develop WebService instances

Related Article

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.