I. Introduction of gSOAP
The gSOAP Compilation tool provides a soap/xml implementation of the C + + language, making it much easier to develop a Web service or client program in the C/D + + language. The vast majority of the C++web Service Toolkit provides a set of API function class libraries to handle specific SOAP data structures, which makes it necessary for users to change the program structure to fit the relevant class library. In contrast, gSOAP uses compiler technology to provide a transparent set of soap APIs and hides content related to the development-agnostic SOAP implementation details to the user.
Second, gSOAP
Http://www.genivia.com/products.html
Download the following folder after unpacking:
Among the two files in gSOAP:
and the following path two files are copied out into a folder, later useful:
After the copy, the total is the following four files:
Third, the realization of simple addition example
Basic function: Implement a simple a+b program, write a program on the server side, which contains the functions of a+b,
The client code then sends two numbers to it, and the server operation gets the results back to the client to display.
1, create a new folder server;
2, the gSOAP decompression out of the four files into the server inside;
3, under the server folder to create a add.h header file, the code is as follows:
<span style= "FONT-SIZE:18PX;" >//GSOAP NS service name:add//gsoap NS service namespace:http://localhost/add.wsdl//gsoap NS service location:http:/ /LOCALHOST//GSOAP NS service executable:add.cgi//gsoap NS service encoding:encoded//gsoap NS schema Namespace:urn:addin T Ns__add (int num1, int num2, int* sum);</span>
The function return value must be int, followed by "ns__" (two underscores), and the last argument must be a reference type or a pointer type (you need to pass the value out).
4, run cmd jump to the server folder, enter the command Soapcpp2.exe add.h, the folder appears as follows (copy one out for later client):
5. Create a new WIN32 console application (Empty) with VC6.0, with the name server (the path to the server folder in front of me is d:/add/server, then this time the project path is d:/add/, the name is server), Create a new Addserver.cpp source file below the project, and the code in the source file is as follows:
<span style= "FONT-SIZE:18PX;" > #include <stdio.h> #include <stdlib.h> #include "stdsoap2.h" #include "add.h" #include "Add.nsmap" # Include "Windows.h" int main (int argc, char* argv[]) {int m, s;struct soap Add_soap;soap_init (&ADD_SOAP);//soap_set_ Namespaces (&add_soap, Add_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);//The sentence describes the server's service soap_end (&ADD_SOAP); }}return 0;} The server-side implementation function is the same as the function declared in Add.h, but with a parameter of the current SOAP connection int ns__add (struct soap *add_soap, int num1, int num2, int *sum) {*sum = Nu M1 + Num2;return 0;} </span>
6, add other files have 7 add.h, soapH.h, SoapStub.h, Stdsoap2.h, SoapC.cpp, SoapServer.cpp, Stdsoap2.cpp,
The 3 source files that will be added in Project-setting have the category set to precompiled Headers (as long as you change one):
7. Add a wsock32.lib after project-setting-link in object/library modules:
8, compile, if there are compile errors at this time, it is estimated that the file add and Add.h generated code two errors, if it is "sockaddr_storage undefined", then you can add the following code (added below the macro):
<span style= "FONT-SIZE:18PX;" >struct sockaddr_storage {U_char sa_len; U_char sa_family; U_char padding[128];}; </span>
There should be no big mistake, and if there is, then you can locate that sentence and comment it out (you can usually do so).
If the compilation succeeds, there will be a Server.exe file in the Debug folder,
Open cmd, go to debug file, enter Server.exe 4567, then enter http://localhost:4567 in IE
, if the XML page is displayed, the program has started.
9, the client code is written, the steps are similar to the server, create a new client folder, all the files up to the 4th step are copied to the client folder, and then create a Win32 console application, named Client (the path to the client folder in front of me is D:/add/client So this time to create the project path is d:/add/, named client), in the project under the new Addclient.cpp source file, the code in the source file is as follows:
#include <stdio.h> #include <stdlib.h> #include "stdsoap2.h" #include "soapH.h" #include "add.nsmap" int Add ( Const char* Server, int num1, int num2, int *sum); int main (int argc, char **argv) {int result =-1; char* server= "http://localhost:4567"; int num1 = 0;int num2 = 0;int sum = 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,err Code =%d\n ", result);} else{printf ("%d+%d=%d\n", NUM1, num2, sum);} return 0;} 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, add_namespaces);//The function is the main function of the client call, the following parameters are declared in the Add.h, 3 more parameters, the function name is the interface function name ns__ Add Front plus//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); return result;}
10. Add other files There are 7 add.h, soapH.h, SoapStub.h, Stdsoap2.h, SoapC.cpp, SoapClinet.cpp, Stdsoap2.cpp, the 3 source files that will be added in Project-setting have the category set to precompiled Headers (unlike server, which is an addedsoapServer.cpp file and the other is adding soapClient.cpp file);
11, after project-setting-link in object/library modules add a wsock32.lib, after compiling, after compiling successfully in the Debug folder will have aclient.exe file;
12, run a cmd jump to server/debug input Server.exe 4567
13. Run another cmd jump to client/debug input client.exe 1 2
There's going to be a 1+2=3.
14, if Server.exe and client.exe two files on two different computers, then the client.exe code char* server= "http://localhost:4567"; that willlocalhost changes to the IP address of the server and then recompile (addclient.cpp).
The use of gSOAP in 20160421 vc++6.0 environment