C + + Implementation webservice__c++

Source: Internet
Author: User
Tags wsdl
First, the introduction
Web service is actually an application that exposes an API that can be invoked through the web, and developers can programmatically implement a functional application through web calls. At a deeper level, Web service is a new branch of Web application that is self-contained, self-describing, modular, and can be described, published, found, and invoked on the Web (typically the web). Web Services offer a number of surprising benefits, with the ability to invoke another Web service across disparate platforms and interoperability, on ubiquitous networks. Because Web services use HTTP protocol communications (typically 80 ports), they can be naturally traversed through firewalls. In addition, we can use a variety of languages to develop and invoke Web services, conducive to enterprise business integration and data reuse.


As mentioned above, we can develop Web services in a variety of languages, but mainly Java and. NET, and there are not many tools for developing Web services with C + +, mainly axis C + + and gSOAP. Compared to the Java version of Axis,axis C + + in the installation of configuration and development deployment of new Web services are more complex, development should focus on every detail, because Axis c++[1] itself does not have detailed log information, when the error is difficult to track which part of the problem. In addition, Axis C + + has strict requirements for the content of WSDL information, and often does not find the problem. Although there are some problems, Axis C + + has an advantage in performance and is therefore preferred by many Web service developers. [2]
For the installation configuration of Axis C + +, refer to its official documentation [1]. In addition, you can use gsoap[3] to develop a C + + Web service. gSOAP is an open source project that can be downloaded from sourceforge.


II. Basic Process
The basic process of developing Web services using Axis C + + can be described briefly as follows:
1. Describing Web services using the Java language
2, using the JAVA2WSDL tool to generate a WSDL file
3. Generate the service-side code framework using the WSDL2WS tool
4, coding to achieve service, and deployment Services
5. Generate client code framework using the WSDL2WS tool
6, coding implementation of the client
Third, the example demonstration
Here is a simple addition add service to illustrate the process of building webservice with Axis C + +.


1. Describing Web services using the Java language
In Axis C + + We have to define the WSDL before we can generate the code framework to implement the service, which is different from Axis java. There are many ways to generate WSDL, you can use the JAVA2WSDL tool, and of course you can write it manually, and here we use the JAVA2WSDL tool to generate it. Here we describe the add service in the Java language:

public interface add{
public int Add (int x, int y);
}


As long as we can declare the method, we do not need to implement the method, so we use the interface to define it. Save the above code as Add.java.


2, using the JAVA2WSDL tool to generate a WSDL file
Javac Add.java (Generate. class file)
java-djava.ext.dirs= $AXISCPP _home/lib/axisjava Org.apache.axis.wsdl.java2wsdl-o add.wsdl-l "Http://localhost/axis /add "-N" http://localhost/axis/Add "Add"


After two steps, we can get the add.wsdl file. It should be explained that the generated WSDL file is sometimes wrong (intf are impl), which could be a bug for the JAVA2WSDL tool. If this happens, you can compare it with the example WSDL file that Axis C + + brings. (Note: It took me two days to find out.)

3. Generate the service-side code framework using the WSDL2WS tool
With the above WSDL file, we can use the WSDL2WS tool to generate the server-side architecture.
java-djava.ext.dirs= $AXISCPP _HOME/LIB/AXISJAVA-CP $AXISCPP _home/lib/axis/wsdl2ws.jar Org.apache.axis.wsdl.wsdl2ws.WSDL2Ws add.wsdl-lc++-sserver

-s server represents the build server code framework
-L C + + represents generating C + + code

4, coding to achieve service, and deployment Services
In the file generated above, we only need to implement the Add method in Add.cpp.
... ...
Xsd__int Add::add (Xsd__int Value0, Xsd__int Value1)
{
return value0+value1;
}
... ...

Compile and chain the code into. So Dynamic library:
g++-shared-i$axiscpp_home/include-o libadd.so *.cpp

To deploy the service, follow these steps:
(1). Copy the generated libadd.so file to the $axiscpp_deploy/lib directory
(2). copy add.wsdl file to $axiscpp_deploy/wsdls directory
(3). Edit the Axiscpp_deploy/etc/server.wsdd file and add the following in the appropriate location:
<service name= "Add" provider= "Cpp:rpc" description= "Add Service" >
<parameter name= "ClassName" value= "/apache/axiscpp/lib/libadd.so"/>
<parameter name= "Allowedmethods" value= "Add"/>
</service>
(4). Restart the HTTP service
(5). Check Http://localhost/axis There is no additional add service


To test whether our published Web service works properly, we use Axis C + + to generate and test client code that invokes the service.


5. Generate client code framework using the WSDL2WS tool
java-djava.ext.dirs= $AXISCPP _HOME/LIB/AXISJAVA-CP $AXISCPP _home/lib/axis/wsdl2ws.jar Org.apache.axis.wsdl.wsdl2ws.WSDL2Ws add.wsdl-lc++-sclient

-S client represents generating client code framework
-L C + + represents generating C + + code


6, coding implementation of the client
The above command generates ADD.HPP, Add.cpp, and then we write the client portal program AddClient.cpp:
#include "add.hpp"
#include <stdio.h>

int main (int argc, char** argv)
{
ADD ws ("Http://localhost/axis/Add");
int sum;
if (argc<2)
{
printf ("usage:%s num1 num2\n", argv[0]);
return-1;
}
sum = Ws.add (atoi (argv[1)), Atoi (argv[2));
printf ("%d\n", sum);
return 0;
}


Compiling the client to generate an executable file:
g++-i$axiscpp_home/include-l$axiscpp_deploy/lib-laxis_client-ldl-o Add *.cpp


You can then use the Add command to test whether the service is working properly.



Iv. Reference Documents
1. Official website: http://ws.apache.org/axis/
2. Using axis C + + to build Web services under Linux: http://www.javayou.com/showlog.jspe?log_id=580
3, with C to achieve webservice:http://www.hnlinux.net/bbs/simple/index.php?t2303.html
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.