这篇文章原文在我的blog : www.breezemind.com
你可以在上面下载到本文的代码。
gSOAP Tutorial : Part I
Introduction:
The gSOAP toolkit provides a SOAP/XML-to-C/C++ language binding toease the development of SOAP/XML Web services and client application inC and C++.The gSOAP uses stub and skeleton compiler to map C or C++types ( both build-in types and user-defined types) into XML datatypes. It’ s a open source toolkit and can be easily ported to lots ofplatform(In fact, I have ported it to ARM7 running uClinux ).
This tutorial will teach you how to build web services using gSOAP.In the part I ,You will learn how to use gSOAP to build both a simpleweb service client and a server. In the part II, we will cover advancedtopic about gSOAP.
Get and Install gSOAP
The latest gSOAP is available on sourceforge . click to download it.
In this tutorial, we use gSOAP 2.7.10. Extract the archive using:
tar -zxvf gsoap_2.7.10.tar.gz
After the extraction, you will get the directory named “gsoap-2.7″。
cd gsoap-2.7/gsoap
ls -l
Here we can see a directory “bin” where pre-compiledgSOAP tools for different platforms ( linux, macosx, win32). For i386Linux, we use tools in directory “linuxi386″ :
- wsdl2h: The WSDL/schema parser tool.
- soapcpp2: The stub/skeleton compiler.
Copy the two tools to where your shell can find them (such as “/usr/local/bin”).
Your first web service application
our first application calculates the sum of two integers . Let’s do it step by step.
note source files in this tutorial can be download code
<script type="text/javascript">
</script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
A. define the interface
int ns_add( int x, int y, int* sum);
note that the return value is the used to determining the status ofSOAP request, parameter x and y are two integer to be added. payspecial attention to the last parameter “sum” which is a “pointer” to sum calculated.
B. Generate stub and skeleton
Run the stub/skeleton compiler
soapcpp2 -c add.h
the option “-c” tells stub/skeleton compiler to generate the C codeinstead of Cpp code. Now you will find files generate by the compilerin current directory. Pay attention to the file soapStub.h withfunction “soap_call_ns_add” declared in it . Your client code willinvoke this function to fulfill a web service.
C. Build the client
with gSOAP, building a SOAP client can be summarized as :
- Init SOAP runtime
- Invoke remote web service method generated by stub/skeleton compiler
// code for client: client.c
#include “soapH.h”
int main()
{
struct soap s;
int sum=0;
int a=3;
int b=4;
soap_init(&s);
if(soap_call_ns_add(&s,”localhost:50000″,NULL,a,b,∑)==SOAP_OK)
{
printf(”%d+%d=%d/n”,a,b,sum);
}
else
{
soap_print_fault(&s,stderr);
}
soap_end(&s);
soap_done(&s);
return 0;
}
remember to add the following lines of code into your clientprogram, namespace is a mapping table used by codes generated by stubcompiler.
struct Namespace namespaces[] =
{
{ “SOAP-ENV”, “http://schemas.xmlsoap.org/soap/envelope/” },
{ “SOAP-ENC”,”http://schemas.xmlsoap.org/soap/encoding/”},
{ “xsi”, “http://www.w3.org/1999/XMLSchema-instance” },
{ “xsd”, “http://www.w3.org/1999/XMLSchema” },
{ “ns”, “urn:Calc”},
{ NULL, NULL }
};
compile the client source files (remeber to copy stdsoap2.h and stdsoap2.c into current directory):
gcc -o client client.c soapC.c soapClient.c stdsoap2.c
D. Build the server
Build a SOAP using code the generated by gSOAP can be summarized as:
- Bind and listen for a client connection
- Process request ( using soap_serve)
code for server: server.c
#include “soapH.h”
int main()
{
struct soap soap;
int m, s; // master and slave sockets
soap_init(&soap);
m = soap_bind(&soap, “localhost”, 50000, 100);
if (m < 0)
soap_print_fault(&soap, stderr);
else
{
fprintf(stderr, “Socket connection successful: master socket = %d/n”, m);
while(1)
{
s = soap_accept(&soap);
if (s < 0)
{
soap_print_fault(&soap, stderr);
break;
}
if (soap_serve(&soap) != SOAP_OK) // process RPC request
soap_print_fault(&soap, stderr); // print error
fprintf(stdout, “request served/n”);
soap_end(&soap); // clean up everything and close socket
}
}
soap_done(&soap); // close master socket and detach environment
}
int ns_add(struct soap* soap, int x, int y,int * sum)
{
*sum=x+y;
return SOAP_OK;
}
struct Namespace namespaces[] =
{
{ “SOAP-ENV”, “http://schemas.xmlsoap.org/soap/envelope/” },
{ “SOAP-ENC”,”http://schemas.xmlsoap.org/soap/encoding/”},
{ “xsi”, “http://www.w3.org/1999/XMLSchema-instance” },
{ “xsd”, “http://www.w3.org/1999/XMLSchema” },
{ “ns”, “urn:Calc”},
{ NULL, NULL }
};
compile the server source files:
gcc -o server server.c soapC.c soapServer.c stdsoap2.c
E. Test
run the server
./server
run the client
./client
you will get the following line on the screen:
3+4=7
Yeah. That is all you need to build a simple service
For more information about gSOAP, please visit the author’s website