〖Linux〗使用gsoap搭建web server(C++)

时间:2024-10-29 16:38:08

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 < )
calc.serve(); /* serve as CGI application */
else
{ int port = atoi(argv[]);
if (!port)
{ fprintf(stderr, "Usage: server <port>\n");
exit();
}
/* run iterative server on port until fatal error */
if (calc.run(port)) /* 运行在port端口上 */
{ calc.soap_stream_fault(std::cerr);
exit(-);
}
}
return ;
} 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 <stdio.h>
#include <stdlib.h>
const char server[] = "127.0.0.1:4567"; int main(int argc, char **argv)
{ if (argc < )
{ fprintf(stderr, "Usage: client num1 num2\n");
exit();
}
int a, b, result;
a = (int)(strtod(argv[], NULL)); /* str转double再转int */
b = (int)(strtod(argv[], 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 ;
}

  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: make OBJ_NS := calc
OBJ_NAME := add
GSOAP_ROOT := /home/scue/work/gsoap_2.8.15/gsoap
INCLUDE := -I$(GSOAP_ROOT) CC := clang++
GCC := clang++
#CC := arm-linux-g++
#CXX := arm-linux-g++ CFLAGS += -w
CXXFLAGS += -w OBJ_SERVER := soapC.o stdsoap2.o soap$(OBJ_NS)Service.o $(OBJ_NAME)server.o
OBJ_CLIENT := soapC.o stdsoap2.o soap$(OBJ_NS)Proxy.o $(OBJ_NAME)client.o all:
@make soap
@make server
@make client server: $(OBJ_SERVER)
$(CXX) $(CXXFLAGS) $(INCLUDE) $^ -o $@
client: $(OBJ_CLIENT)
$(CXX) $(CXXFLAGS) $(INCLUDE) $^ -o $@ .PHONY:soap
soap:
@cp -v $(GSOAP_ROOT)/stdsoap2.* .
@$(GSOAP_ROOT)/bin/linux386/soapcpp2 -i $(OBJ_NAME).h soapqt:
@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:clean
clean:
rm -f server client *.o
distclean:
rm -f server client *.o ns* soap* *.xml *.nsmap *.wsdl stdsoap2.*

  5)编译及试验

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

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