首先是在网上看到gSoap并不知道是什么于是想研究一下,然后就在虚拟机安装了gSoap
系统环境是乌班图18.04发布版。
gSoap源码地址:
gSOAP: Modified gSOAP library for use by Ardupilot
安装参考:
INSTALL.txt · kylinSama/gSOAP - 码云 - 开源中国 (gitee.com)
安装好以后就迫不及待的根据教程写代码测试、这里是官方的入门教程
Genivia - C/C++ XML 数据绑定和 XML SOAP/REST Web Services 入门
首先创建一个hello.h 文件
在其中添加内容int ns__hello(std::string name, std::string& greeting);保存并关闭
然后执行soapcpp2 hello.h 可以得到一堆文件 详细可以参考教程这里直接说我后面的过程
然后创建hello.cpp 文件 并添加代码:
// hello.cpp
#include "soapH.h" // include the generated source code headers
#include "ns.nsmap" // include XML namespaces
int main()
{
return soap_serve(soap_new());
}
int ns__hello(struct soap *soap, std::string name, std::string& greeting)//这里是实现 hello.h中定义的函数 惨数必须多一个struct soap *soap 说是用来连接上下文 这里我还不懂后面研究
{ greeting = "Hello " + name; return SOAP_OK; }
然后保存关闭文件
在编译服务器程序c++ -o hello.cgi hello.cpp soapC.cpp soapServer.cpp -lgsoap++
如果这调指令不行就用这条
c++ -o hello.cgi hello.cpp soapC.cpp soapServer.cpp stdsoap2.cpp//注意这里的stdsoap2.cpp需要去gSoap源码中拷过来
然后服务器文件就有了。
然后在创建客户端代码
#include "soapH.h"
#include "ns.nsmap" // include XML namespaces
int main(){
struct soap *soap = soap_new(); // new context
std::string greeting;
if (soap_call_ns__hello(soap, "http://localhost/cgi-bin/hello.cgi", NULL, "world", greeting) == SOAP_OK)
std::cout << greeting << std::endl;
else
soap_stream_fault(soap, std::cerr);
soap_destroy(soap); // delete managed deserialized C++ instances
soap_end(soap); // delete other managed data
soap_free(soap);
}。//注意这里的"http://localhost/cgi-bin/hello.cgi"要替换成你的路径 这里就是后面遇到的最大的问题 后面详细说
先编译客户端程序c++ -o myapp myapp.cpp soapC.cpp soapClient.cpp -lgsoap++(同样的没有gsoapp库就改为stdsoap2.cpp)
然后客户端和服务器就都好了
按照我初学者的理解就是 首选运行刚才编译好的服务器程序hello.cgi 然后在另一个终端中运行客户端程序myapp 结果就是报了一堆html复制出来在浏览器中看了下就是在浏览器中显示了一个world 这个结果是错的正确的运行结果是在控制台输出一个 hello world,于是我就开始了网上漫长的搜索 直到第二天搜到了一个前辈的文章
gSOAP 入门实践(一)_gsoap genivia-CSDN博客
才知道原来还需要配置httpd 把hello.cig放到指定的文件夹才能访问,于是网上搜索httpd说是需要安装Apache2, 安装的过程就不细说了网上很多,然启动了 systemctl start apache2 。我以为这样就行了,于是又运行 客户端程序myapp 但是还是报错,于是又在网上搜了很久 最后尝试了各种办法,最后终于找到了正确的配置方法,首先需要修改两个配置文件/etc/apache2/apache2.conf
/和etc/apache2/sites-available/000-default.conf
在其中添加ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI
Require all granted
</Directory>
然后创建文件夹sudo mkdir /usr/lib/cgi-bin 把hello.cig复制到这个文件夹下面后 重启服务
sudo service apache2 restart
在执行就ok了
不懂的地方还有很多 继续研究 这里就记录一下怎么跑通的