博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
〖Linux〗使用gsoap搭建web server(C++)
阅读量:7021 次
发布时间:2019-06-28

本文共 3481 字,大约阅读时间需要 11 分钟。

1. gsoap的好处就不用说了:

2. gsoap的下载地址:,目前我使用的是2.8.15版本

3. 开发环境:Ubuntu13.10

4. 具体操作步骤(以简单相加为例):

  1)编写add.h(头文件)

//gsoap ns service name: calc//gsoap ns service protocol: SOAP//gsoap ns service style: rpc//gsoap ns service encoding: encoded//gsoap ns service namespace:    http://localhost:8888//gsoap ns service location:    http://localhost:8888//gsoap ns service port:    http://localhost:8888 int ns__add( int num1, int num2, int* sum );

  2)编写addserver.cpp(服务器)

#include "soapcalcService.h"                    /* 与add.h中第一行ns有关 */#include "calc.nsmap"                           /* 与add.h中第一行ns有关 */int main(int argc, char **argv){ calcService calc;                             /* 创建calc对象 */  if (argc < 2)    calc.serve();    /* serve as CGI application */  else  { int port = atoi(argv[1]);    if (!port)    { fprintf(stderr, "Usage: server 
\n"); exit(0); } /* run iterative server on port until fatal error */ if (calc.run(port)) /* 运行在port端口上 */ { calc.soap_stream_fault(std::cerr); exit(-1); } } return 0;} int calcService::add(int a, int b, int *result) /* 在此处实现add() */{ *result = a + b; return SOAP_OK;}

  3)编写addclient.cpp(客户端)

#include "soapcalcProxy.h"                      /* 与add.h第一行的ns有关 */#include "calc.nsmap"                           /* 与add.h第一行的ns有关 */#include 
#include
const char server[] = "127.0.0.1:4567";int main(int argc, char **argv){ if (argc < 3) { fprintf(stderr, "Usage: client num1 num2\n"); exit(0); } int a, b, result; a = (int)(strtod(argv[1], NULL)); /* str转double再转int */ b = (int)(strtod(argv[2], NULL)); calcProxy calc; /* 创建calc对象 */ calc.soap_endpoint = server; /* 设定server */ calc.add(a, b, &result); /* 执行add() */ if (calc.error) calc.soap_stream_fault(std::cerr); else printf("result = %d\n", result); /* 打印消息 */ return 0;}

  4)编写Makefile文件

# this is a Makefile to build client and server# please setting the GSOAP_ROOT first.# build procedure(EXAMPLE): server run on pc, then client run on arm#     step1: setting GSOAP_ROOT#     step2: setting OBJ_NS   # the first line in add.h#     step3: setting OBJ_NAME  # the basename of filename of add.h#     step4: setting CC and CXX#     step4: makeOBJ_NS := calcOBJ_NAME := addGSOAP_ROOT := /home/scue/work/gsoap_2.8.15/gsoapINCLUDE := -I$(GSOAP_ROOT)CC := clang++GCC := clang++#CC := arm-linux-g++#CXX := arm-linux-g++CFLAGS += -wCXXFLAGS += -wOBJ_SERVER := soapC.o stdsoap2.o soap$(OBJ_NS)Service.o $(OBJ_NAME)server.oOBJ_CLIENT := soapC.o stdsoap2.o soap$(OBJ_NS)Proxy.o $(OBJ_NAME)client.oall:     @make soap    @make server    @make client server: $(OBJ_SERVER)    $(CXX) $(CXXFLAGS) $(INCLUDE) $^ -o $@client: $(OBJ_CLIENT)    $(CXX) $(CXXFLAGS) $(INCLUDE) $^ -o $@.PHONY:soapsoap:    @cp -v $(GSOAP_ROOT)/stdsoap2.* .    @$(GSOAP_ROOT)/bin/linux386/soapcpp2 -i $(OBJ_NAME).hsoapqt:    @mkdir -p $(OBJ_NS)qtclient    @cp -v soapH.h soapStub.h stdsoap2.h soap$(OBJ_NS)Proxy.h \        soapC.cpp stdsoap2.cpp soap$(OBJ_NS)Proxy.cpp $(OBJ_NS).nsmap \        $(OBJ_NS)qtclient/# -c 生成C的文件# -i 生成C++的文件.PHONY:cleanclean:    rm -f server client *.odistclean:    rm -f server client *.o ns* soap* *.xml *.nsmap *.wsdl stdsoap2.*

 

  5)编译及试验

make./server 4567./client 2 3 #将会返回5,也可以直接在浏览器中输入http://localhost:4567进行验证

总结:使用C++编写将会轻松很多。

转载于:https://www.cnblogs.com/scue/p/3402366.html

你可能感兴趣的文章
DDOS防御总结
查看>>
rem windows服务器时间同步
查看>>
Android 获取手机IMEI方法
查看>>
Linux应用开发自学之路
查看>>
windows8小技巧之快捷键
查看>>
python(运算符)
查看>>
tomcat的安装
查看>>
PHP教程:详解PHP归并排序的实现
查看>>
云计算应用场景有哪些?
查看>>
防止SQL注入解决方案
查看>>
python新手入门常犯的错误
查看>>
javascript之this指向
查看>>
FTP实时更新上传脚本
查看>>
awk中多个分隔符^识别办法
查看>>
Centos7 初始化MySQL5.7数据库
查看>>
从零开始的linux 第十八章
查看>>
c#获取逻辑硬盘信息
查看>>
vSphere虚拟化之vClient安装虚拟机
查看>>
23种设计模式介绍(二)---- 结构型模式
查看>>
IOT物联网观察之商业本质是价值交换,物量经济交换逻辑是什么?
查看>>