使用C++编写web services
2008-05-31 23:12
204 查看
这篇文章原文在我的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
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
相关文章推荐
- 使用C++与SFML编写一个简单的撞球游戏Part6——添加弹板控制与弹球
- Linux下使用C++来编写PHP扩展库
- 用C++编写dll并使用
- 在C++中使用C#编写的类2
- 使用C++编写Jni库进行总结
- Silverlight实用窍门系列:22.Silverlight使用WebService调用C++,Delphi编写的DLL文件【实例源码下载】
- QTC++ widget中使用QML编写的UI界面
- 一元二次方程求解 使用c++ class编写
- 8、ROS使用C++编写一个简单的Publisher和Subscriber
- 如何减少C++编写程序的CPU使用率
- 使用C++编写类似MFC的CString,部分功能,重新复习使用操作符重载
- 在C#中使用C++编写的类
- 使用C++编写简单的迷宫游戏
- 使用eclipse调试cocos2d-x C++ 代码(编写+真机调试,放弃VS)
- 在C#中使用C++编写的类(zt)
- 在C++中使用C#编写的类
- 编写高效的C++程序方法之使用对象池
- 使用CDT+cygwin编写C/C++
- 在C#中使用C++编写的类——用托管C++进行封装[转]
- Linux下,使用C/C++编写"静态链接库"的一个简单例子