用 C++ 实现一个独立的 Web Service 程序

0. 序

这一次我创建一个名为 calc 的 Web Service 程序,功能很简单,实现一个整数 add 函数。

1. 创建源代码

我创建了一个 calc 目录,里面创建下面两个文件:

calc.h:

int ns__add(int x, int y, int &result);

很奇葩的是,函数 add 之前必须加上 ns__,否则后续工作无法继续。看官网的例子,加上 ns2__ 前缀也行。具体什么原因有知道的可以留言补充一下。

calc.cpp

#include "ns.nsmap"
#include "soapService.h" // generated with soapcpp2 -j calc.h

int port = 8080;

int main()
{
  Service calc;                                        // service class generated with soapcpp2 -j
  calc.soap->accept_timeout = 24*60*60;                  // quit after 24h of inactivity (optional)
  calc.soap->send_timeout = calc.soap->recv_timeout = 5; // max send and receive socket inactivity time (sec)
  calc.soap->transfer_timeout = 10;                      // max time for send or receive of messages (sec)
  while (calc.run(port))                                 // bind, then loop to accept and serve requests
    calc.soap_stream_fault(std::cerr);
  calc.destroy();                                        // delete deserialized objects and temp data
  return 0;
}

int Service::add(int x, int y, int &result)
{
	result = x + y;
	return SOAP_OK;
}

代码 Service calc; ,官方的例子中是 Service calc(); 但是,这样子用 g++ 编译不过去,因为类的构造函数没参数,不能用空括号。

2. 生成辅助源代码

calc.nsmapsoapcalcProxy.h 这两个文件是利用程序 soapcpp2 生成的,命令如下:

$ soapcpp2 -j -SL -I/home/jetson/gsoap-2.8/gsoap/import calc.h

**  The gSOAP code generator for C and C++, soapcpp2 release 2.8.114
**  Copyright (C) 2000-2021, Robert van Engelen, Genivia Inc.
**  All Rights Reserved. This product is provided "as is", without any warranty.
**  The soapcpp2 tool and its generated software are released under the GPL.
**  ----------------------------------------------------------------------------
**  A commercial use license is available from Genivia Inc., contact@genivia.com
**  ----------------------------------------------------------------------------

Saving soapStub.h annotated copy of the source interface header file
Saving soapH.h serialization functions to #include in projects
Using ns service name: Service
Using ns service style: document
Using ns service encoding: literal
Using ns schema namespace: http://tempuri.org/ns.xsd
Saving ns.wsdl Web Service description
Saving soapService.h service class
Saving soapService.cpp service class
Saving ns.add.req.xml sample SOAP/XML request
Saving ns.add.res.xml sample SOAP/XML response
Saving ns.xsd XML schema
Saving ns.nsmap namespace mapping table
Saving soapC.cpp serialization functions

Compilation successful

3. 编译

输入下面的命令行:

$ g++ -o calc calc.cpp soapC.cpp soapService.cpp  stdsoap2.cpp

成功了!

4. 运行服务器

首先运行 calc 程序:

$ ./calc

服务器就运行起来了。

5. 客户端测试

下载了一份 soapUI 客户端测试程序,运行,从菜单选择创建 soap 项目:

在这里插入图片描述

其中 Initial WSDL,可以用 Browse 按钮浏览自动生成的 ns.wsdl 文件,结果如下:

在这里插入图片描述

点击 Ok。

点击主窗口左上角的 Projects/ns/Service/add/Request 1,

在这里插入图片描述

弹出测试窗口。设置服务地址,并把函数 add 参数 x、y 的问号换成具体数值,比如 10 和 20, 点击左上角播放按钮,得到测试结果:

在这里插入图片描述

我们看到,右侧结果是 30。

6. 结论

gSoap 官网文档,不知道是在什么机器上测试的,在我的 Jetson Nano 环境下困难重重。经过这一番测试,总算顺利搞定。C/C++ 开发 Web Service 还是不错的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

许野平

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值