gSOAP:C++编写服务器端

1.编写头文件cal.h:

//gsoap ns service name: calc
//gsoap ns service style: rpc
//gsoap ns service encoding: encoded
//gsoap ns service namespace: http://127.0.0.1:8089/calc.wsdl
//gsoap ns service location: http://127.0.0.1:8089/cal
//gsoap ns schema  namespace:    urn:calc
int ns__add(double a, double b, double *result);
int ns__sub(double a, double b, double *result);
int ns__mul(double a, double b, double *result);
int ns__div(double a, double b, double *result);
int ns__pow(double a, double b, double *result);

2.生成文件:

soapcpp2.exe -i cal.h

3.将以下文件拷入项目:

拷入文件

4.编写gSOAPService.cpp:

#define _CRT_SECURE_NO_WARNINGS   **//一定要添加上**
#include "calc.nsmap" 
#include"soapcalcService.h"
#include "iostream"  //控件问提只能写“”

using namespace std;

//很重要
int   http_get(struct   soap   *soap)
{
    FILE*fd = NULL;
    fd = fopen("C:\Users\Hunter\Documents\Visual Studio 2015\Projects\Service\Debug\calc.wsdl", "rb"); //open WSDL file to copy

    if (!fd)
    {
        return 404; //return HTTP not found error
    }
    soap->http_content = "text/xml";  //HTTP header with text /xml content
    soap_response(soap, SOAP_FILE);
    for (;;)
    {
        size_t r = fread(soap->tmpbuf, 1, sizeof(soap->tmpbuf), fd);
        if (!r)
        {
            break;
        }
        if (soap_send_raw(soap, soap->tmpbuf, r))
        {
            break; //cannot send, but little we can do about that
        }
    }
    fclose(fd);
    soap_end_send(soap);
    return SOAP_OK;
}


int main(int argc, char *argv[])
{
    calcService cal;
    cal.fget = http_get;
    while (1)
    {
        if (cal.run(8089))
        {
            cal.soap_stream_fault(std::cerr);
        }
    }
    return 0;
}

//自动生成了calcService类,自己重写add等函数
/*加法的具体实现*/
int calcService::add(double num1, double num2, double* result)
{
    if (NULL == result)
    {
        printf("Error:The third argument should not be NULL!
");
        return SOAP_ERR;
    }
    else
    {
        (*result) = num1 + num2;
        return SOAP_OK;
    }
    return SOAP_OK;
}

/*减法的具体实现*/
int calcService::sub(double num1, double num2, double* result)
{
    if (NULL == result)
    {
        printf("Error:The third argument should not be NULL!
");
        return SOAP_ERR;
    }
    else
    {
        (*result) = num1 - num2;
        return SOAP_OK;
    }
    return SOAP_OK;
}

/*乘法的具体实现*/
int calcService::mul(double num1, double num2, double* result)
{
    if (NULL == result)
    {
        printf("Error:The third argument should not be NULL!
");
        return SOAP_ERR;
    }
    else
    {
        (*result) = num1 * num2;
        return SOAP_OK;
    }
    return SOAP_OK;
}

/*除法的具体实现*/
int calcService::div(double num1, double num2, double* result)
{
    if (NULL == result || 0 == num2)
    {
        return soap_senderfault("Square root of negative value", "I can only compute the square root of a non-negative value");
        return SOAP_ERR;
    }
    else
    {
        (*result) = num1 / num2;
        return SOAP_OK;
    }
    return SOAP_OK;
}

int calcService::pow(double num1, double num2, double* result)
{
    if (NULL == result || 0 == num2)
    {
        printf("Error:The second argument is 0 or The third argument is NULL!
");
        return SOAP_ERR;
    }
    else
    {
        (*result) = num1 / num2;
        return SOAP_OK;
    }
    return SOAP_OK;
}

测试一:

http://127.0.0.1:8089/calc.wsdl

测试二:

运行服务器程序,并执行:

wsdl2h.exe -s -o cal.h http://127.0.0.1:8089/calc.wsdl
soapcpp2.exe -i cal.h

添加引用
stdsoap2在gSOAP目录中搜索。
test.cpp:

#include"soapcalcProxy.h"
#include"calc.nsmap"
#include"iostream"

using namespace std;

int main()
{
    calcProxy p;
    double result = 0;
    if (SOAP_OK == p.add(1, 2, result))
    {
        cout << result << endl;
    }
    else
    {
        p.soap_stream_fault(std::cerr);
    }
    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/ggzone/p/4429920.html