在C++中实现Webservice

      最近正在整理以前开发的产品, 发现在很久以前写一篇关于webservie开发的文章<>, 见 http://hi.csdn.net/link.php?url=http://blog.csdn.net%2Fhjy82919. 对这一篇文章感觉比较泛,所以打算今天配合实例详细的说明一下.

        1. 首先我们编写一个自己将提供的接口文件webservice_Test.h, 内容如下:

      int ns__Add(long in1 , long in2, long* result);

在这个文件中就只包含了一行, 这一行定义了ns__Add函数,用于把两个输入进行相加并返回结果.

2.  使用Soap2cpp.exe同时生成webservice的客户端和服务端,命令如下:

           soapcpp2 -L -x -i webservice_test.h            (-C: 只生成客户端, –S: 只生成服务端)

  相应的会生成14个文件, 主要有:soapWebservice_TestService.h, soapWebservice_TestService.cpp,

                                          soapWebservice_TestProxy.h,  soapWebservice_TestProxy.cpp,

                                          soapC.cpp, soapH.h, soapStub.h, Webservice_Test.wsdl, Webservice_Test.nsmap

   其中soapWebservice_TestService.h文件内容如下:

/* soapWebservice_TestService.h
   Generated by gSOAP 2.7.9l from ./test/webservice_test.h
   Copyright(C) 2000-2007, Robert van Engelen, Genivia Inc. All Rights Reserved.
   This part of the software is released under one of the following licenses:
   GPL, the gSOAP public license, or Genivia's license for commercial use.
*/

#ifndef soapWebservice_TestService_H
#define soapWebservice_TestService_H
#include "soapH.h"
class SOAP_CMAC Webservice_TestService : public soap
{ public:
    /// Constructor
    Webservice_TestService();
    /// Constructor with engine input+output mode control
    Webservice_TestService(soap_mode iomode);
    /// Constructor with engine input and output mode control
    Webservice_TestService(soap_mode imode, soap_mode omode);
    /// Destructor frees all data
    virtual ~Webservice_TestService();
    /// Initializer used by constructor
    virtual    void Webservice_TestService_init(soap_mode imode, soap_mode omode);
    /// Return a copy
    virtual    Webservice_TestService *copy();
    /// Disables and removes SOAP Header from message
    virtual    void soap_noheader();
    /// Run simple single-thread iterative service on port until a connection error occurs (returns error code or SOAP_OK), use this->bind_flag = SO_REUSEADDR to rebind for a rerun
    virtual    int run(int port);
    /// Bind service to port (returns master socket or SOAP_INVALID_SOCKET)
    virtual    SOAP_SOCKET bind(const char *host, int port, int backlog);
    /// Accept next request (returns socket or SOAP_INVALID_SOCKET)
    virtual    SOAP_SOCKET accept();
    /// Serve this request (returns error code or SOAP_OK)
    virtual    int serve();
    /// Used by serve() to dispatch a request (returns error code or SOAP_OK)
    virtual    int dispatch();
    /// Service operations (you should define these):
    /// Web service operation 'Add' (return error code or SOAP_OK)
    virtual    int Add(long in1, long in2, long *result);
};
#endif

   
Webservice_TestService :: Add() 即为我们提供的服务函数.

 

把soapWebservice_TestService.h, soapWebservice_TestService.cpp, soapC.cpp, soapH.h, soapStub.h, Webservice_Test.nsmap

以及stdsoap2.h,stdsoap2.cpp这两个文件添加到工程中.

4. 编程

   只需要在soapWebservice_TestService.cpp中实现Webservice_TestService :: Add()这个函数,如下

  int Webservice_TestService :: Add( long in1, long in2, long *result )

{

        result = in2 + in2;

        return SOAP_OK;

}

5. 启动webservice服务

   生成Webservice_TestService的一个对象,对调用Webservice_TestService的run函数(用于单线程,如果需要支持多线程,则需要另外提供多线程控制).

       Webservice_TestService  service;

       service.run( 8080 );   // 8080为 webservice监听的端口, 对应的客户端调用服务时的地址为: http://serverIP:8080

       到此,webservice服务的开发就已完成.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值