Win7 gSOAP and vs2010 C + + Create a Web Service

Source: Internet
Author: User
Tags soap square root

---restore content starts---

I have previously written a simple sample, long time no touch, found that all have forgotten, and now need to re-consolidate.

The first is to download gSOAP, unable to access the official download page, can only search the Internet, found a 2.8 version of the cloud disk in order to avoid being found again.

The process of creating a Web service through gSOAP is documented below.

1. Create a project folder Calc

2. In the Calc folder, create a header file Calc.cpp

//Contents of File "calc.h"://gsoap ns Service Name:calculator//gsoap ns Service Style:rpc//gsoap ns Service encoding:encoded//gsoap ns service port:http://localhost: 8080/calc/calculator.cgi//gsoap ns Service Namespace:urn:calculatorintNs__add (DoubleADoubleBDouble&result); intNs__sub (DoubleADoubleBDouble&result); intNS__SQRT (DoubleADouble&result);

Note that the comment section is not omitted, and this section records some important configuration information.

3. Download the gSOAP compressed package, get the Stdsoap.cpp and Stdsoap.h files in/gsoap-2.8/gsoap/(Note: This article uses C + + as a stdsoap.cpp file, if you use C you should select the Stdsoap.c file. This file provides the declaration and implementation of the required functions in the Web Service project. Get/gsoap-2.8/gsoap/bin/win32/ Soapcpp2.exe (used to parse the created header file) and put it in the Calc folder where Calc.h is located (easy to execute the command line, and of course it is not necessary to place the cmd command more complex: soapcpp2 (calc.h path)/calc.h-i ( Specifies the build file directory).

4. Start the cmd form, switch to the directory where Soapcpp2.exe is located, execute soapcpp2 calc.h generate the following file:

One of the calc.cpp is the later self-added files, the SOAPPCPP2 program is convenient for the operation, before adding in.

Note: the use of commands on SOAPCPP2 can be viewed through soapcpp2-h.

4. Start the creation of the calculator (Win32 console application) solution (currently the default added project is calculator, this article defines it as the server side, and later adds the project definition as client side).

For simplicity, create an empty project here.

5. Make network communication and add WSock32.lib.

Results such as:

Click Apply, OK to add complete.

6. Open the project directory and add the following files:

Where the calc.cpp content is as follows (Calc.h method implementation):

#include"soapH.h"#include"Calculator.nsmap"#include<math.h>intMainintargcChar**argv) {    intm, S; structsoap 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]), -); 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); }    }    //Soap_serve (Soap_new ());    return 0;}//implementation of the "Add" remote method:intNs__add (structSoap *soap,DoubleADoubleBDouble&result) {Result= A +b; returnSOAP_OK;} //implementation of the "sub" remote Method:intNs__sub (structSoap *soap,DoubleADoubleBDouble&result) {Result= A-b; returnSOAP_OK;} //implementation of the "SQRT" remote method:intNS__SQRT (structSoap *soap,DoubleADouble&result) {    if(A >=0) {result=sqrt (a); returnSOAP_OK; }    Else   {       returnSoap_sender_fault (SOAP,"Square root of negative value","I can only compute the square root of a non-negative value"); } } 

7. Add files to your project:

Where Calc.cpp is a new item, the rest is an existing item, and the final result

8. Generate the project, error is as follows;

Search for a workaround as follows:

9. Right-click the project and regenerate it.

10. Right-click the solution, create a new Calcclient project, the same as an empty project, the steps are the same as the creation of the server side, you also need to add the WS32.lib, different when the required files are added, the file is as follows:

11.calcclient.cpp as follows:

#include"SoapStub.h"#include"Calculator.nsmap"  intAddConst Char*server,intNUM1,intNUM2,Double&sum) {      structsoap Add_soap; intresult =0; Soap_init (&add_soap); Soap_set_namespaces (&Add_soap, namespaces); Soap_call_ns__add (&add_soap, Server, NULL, NUM1, num2, sum); printf ("Server is%s, Num1 was%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); returnresult; } 

CalcClientTest.cpp as follows:

#include <stdio.h>#include<stdlib.h>#include<string.h>intAddConst Char*server,intNUM1,intNUM2,Double&sum); intMainintargcChar**argv) {          intresult =-1; Charserver[ -] = {0}; intNUM1; intnum2; Doublesum; 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 =%f \ n", NUM1, num2, sum); }          return 0; }  

The final project structure is as follows:

12. Modify the input and output in the Inventory tool, and change the embed list to "no", otherwise it will be reported as the previous link error.

13. Generate client side.

14. Run server and client.

Start cmd switch to Calculator.exe directory, execute: Calculator 8080 start server side

Start cmd switch to CalcClient.exe directory, execute: calcclient localhost:8080 7 7 Request Server Side

Summary:

Now is the soap and C + + beginners, many still do not understand, walk through this program, I have to remember. The main difficulties are as follows:

1) When creating server and client side, I do not know which files to add (should check the literature first to see what the resulting files do).

2) When adding WSock32.lib, I don't know how to add it (query How to add Lib in vs Project).

3) When you build the project, there is a link error (the method of modifying the Inventory tool on the web is not as good as it should be, so take a good look later).

4) for gSOAP configuration information is not known, need to be well mended.

Doubt:

1) is the//GSOAP NS service port:http://localhost:8080 configured in calc.h useful, and how do I need to bind the port number when I start the server side?

2) Whether the generated WSDL file and the NS file are useful, and the two files are not used in the server and client projects.

Reference article:

1)Onvif Learning 2-soap Introduction and gSOAP use

2) gSOAP Calculator Service and Client

3) The gSOAP Toolkit for SOAP and REST Web Services and xml-based applications

Win7 gSOAP and vs2010 C + + Create a Web Service

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.