gsoap linux下服务器端代码生成与测试

1、下载gsoap代码下载链接 https://download.csdn.net/download/u011186256/10711057

2、解压压缩包。

3、进入cd gSoap/gsoap-2.8/gsoap/bin/linux386

4、创建一个头文件serverTest.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__hello(char *p, char *result);
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);

5、./soapcpp2 -i serverTest.h

生成需要的文件

6、中calc.wsdl、soapC.cpp、soapStub.h、soapcalcService.h、stdsoap2.h、 calc.nsmap、serverTest.h、soapH.h  、  soapcalcService.cpp、 stdsoap2.cpp 是需要的文件。

7、创建main.cpp文件

#include "iostream"
#include "soapcalcService.h"
#include "stdafx.h"
#include "calc.nsmap"

#include <stdio.h>

using namespace std;

//º??ª
int  http_get(struct   soap   *soap)
{
        FILE*fd = NULL;
        fd = fopen("./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;
}

8、编写makefile

OBJ_NS := calc
GSOAP_ROOT := /home/likaiqiang/test/gSoap/gsoap-2.8/gsoap/bin/linux386/server/testCore
INCLUDE := -I$(GSOAP_ROOT)

#GCC := g++
CXX := arm-linux-gnueabihf-g++

CFLAGS += -w
CXXFLAGS += -w

OBJ_SERVER := stdafx.o soapC.o stdsoap2.o soap$(OBJ_NS)Service.o

server: $(OBJ_SERVER)
 $(CXX) $(CXXFLAGS) $(INCLUDE) $^ -o $@

clean:
 rm -f server client *.o

9、编译运行,出错提示没有实现头文件里面的五个函数的实现

soapcalcService.o:(.rodata._ZTV11calcService[_ZTV11calcService]+0xc0): undefined reference to `calcService::hello(char*, char*)'
soapcalcService.o:(.rodata._ZTV11calcService[_ZTV11calcService]+0xc8): undefined reference to `calcService::add(double, double, double*)'
soapcalcService.o:(.rodata._ZTV11calcService[_ZTV11calcService]+0xd0): undefined reference to `calcService::sub(double, double, double*)'
soapcalcService.o:(.rodata._ZTV11calcService[_ZTV11calcService]+0xd8): undefined reference to `calcService::mul(double, double, double*)'
soapcalcService.o:(.rodata._ZTV11calcService[_ZTV11calcService]+0xe0): undefined reference to `calcService::div(double, double, double*)'
soapcalcService.o:(.rodata._ZTV11calcService[_ZTV11calcService]+0xe8): undefined reference to `calcService::pow(double, double, double*)'
collect2: error: ld returned 1 exit status
make: *** [server] Error 1

10、在soapcalcService.cpp文件中实现相关函数


int calcService::hello(char *p, char *result)
{
 if (NULL == p || NULL == result)
 {
  printf("Error:The third argument should not be NULL!\n");
  return SOAP_ERR;
 }
 else
 {
  printf("recv :%s\n",p);
  memcpy(result,"hello world",16);
  return SOAP_OK;
 }
 return SOAP_OK;
}

int calcService::add(double num1, double num2, double* result)
{
 if (NULL == result)
 {
  printf("Error:The third argument should not be NULL!\n");
  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!\n");
  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!\n");
  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!\n");
  return SOAP_ERR;
 }
 else
 {
  (*result) = num1 / num2;
  return SOAP_OK;
 }
 return SOAP_OK;
}

11、运行./server

12、测试http://127.0.0.1:8089/cacl.wsdl

 

参考链接:https://blog.csdn.net/u011186256/article/details/82994524

参考链接:https://blog.csdn.net/ggz631047367/article/details/44567411

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值