当前位置:网站首页>Win10 build webservice
Win10 build webservice
2022-06-24 07:37:00 【Hard working Lao Zhou】
Preface
Because the project needs to use WebService, It's my first time to use WebService, Belong to completely inexperienced . Thanks to the universal Ethernet . The following is about the first construction WebService The server side writes a simple summary .
Development environment construction
System environment
Win10 + gSoap 2.8.119
The server-side program currently uses VS2019.
gSoap install
download gSoap
gSoap Just download directly from the network , No local compilation is required , The download is a little slow . Download at :
https://sourceforge.net/projects/gsoap2/files/.
I chose the latest version , That is to say gSoap 2.8.119.
decompression
After downloading is a zip file , It can be extracted directly . I unzipped it in d:/gsoap Under the table of contents . The executables we need to use are 
Add a path
take D:\gsoap-2.8\gsoap\bin\win64 Add to system environment variables . Here's the picture .
such ,gSoap Just install it .
gSoap Introduction to important tools
wsdl2h
The main function of the tool is , adopt wsdl File generation C/C++ .h The header file .
socapcpp2
This tool is used to create files from scratch , Generate SOAP Server and client code , It also includes WSDL、 Test use XML data .
first Web Service Server side
When I first read the Internet materials , Need to build a wsdl file , Also download from the network , See yourself completely at a loss . Fortunately, you can find a data and set up a complete server by yourself .
Create a server folder
I am here d:\gsoap-2.8\gsoap. Here's the picture . Created a gServer Catalog .
This directory is anywhere .
Create a header file to provide services
We use one of the most common calculator functions to implement Web Service Server side , Provide some basic operations .
#ifndef __GSERVICE_H__
#define __GSERVICE_H__
//gsoap ns service name: gservice
//gsoap ns service style: rpc
int ns__add(double num1, double num2, double& result );
int ns__sub(double num1, double num2, double& result );
int ns__mult(double num1, double num2, double& result);
int ns__divid(double num1, double num2, double& result);
#endif
The current folder is as follows .
Generate wsdl file
Use the following command line ,
soapcpp2.exe -S gservice.h
To generate wsdl Wait for the documents . The complete recording process is as follows .
D:\gsoap-2.8\gsoap\gServer>soapcpp2.exe -S gservice.h
** The gSOAP code generator for C and C++, soapcpp2 release 2.8.119
** 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., [email protected]
** ----------------------------------------------------------------------------
Saving soapStub.h annotated copy of the source interface header file
Saving soapH.h serialization functions to #include in projects
Using ns service name: gservice
Using ns service style: rpc
Using ns service encoding: literal
Using ns schema namespace: http://tempuri.org/ns.xsd
Saving gservice.wsdl Web Service description
Saving gservice.add.req.xml sample SOAP/XML request
Saving gservice.add.res.xml sample SOAP/XML response
Saving gservice.sub.req.xml sample SOAP/XML request
Saving gservice.sub.res.xml sample SOAP/XML response
Saving gservice.mult.req.xml sample SOAP/XML request
Saving gservice.mult.res.xml sample SOAP/XML response
Saving gservice.divid.req.xml sample SOAP/XML request
Saving gservice.divid.res.xml sample SOAP/XML response
Saving gservice.nsmap namespace mapping table
Saving ns.xsd XML schema
Saving soapServer.cpp server request dispatcher
Saving soapServerLib.cpp server request dispatcher with serializers (use only for libs)
Saving soapC.cpp serialization functions
Compilation successful
such , We have generated what we need gservice.wsdl,gservice.nsmap. As shown in the figure below .
Use vs2019 Set up the service end project
I use the vs2019, stay D:\gsoap-2.8\gsoap\gServer A console application is generated under the directory .

And I'm gonna go ahead and create . The generated blank project is shown in the following figure .

Copy necessary files
take stdsoap2.h,stdsoap2.c,stdsoap2.cpp copy to gServer Under the table of contents , These three documents are gSoap Provided , stay D:\gsoap-2.8\gsoap Under the table of contents .
Add the necessary header file
And will stdsoap2.h、soapStub.h、soapH.h、gservice.nsmap and stdsoap2.cpp、soapC.cpp、soapServer.cpp File import to gServer in , The project directory structure is as follows .
modify gServer.cpp
increase soap Related codes
stay main() add soap service , Port, etc .
// gServer.cpp : This file contains "main" function . Program execution will start and end here .
//
#include <iostream>
#include "stdio.h"
#include "../soapH.h"
#include "../gservice.nsmap"
int main(int argc, char** argv)
{
int nPort = 8080;
struct soap fun_soap;
soap_init(&fun_soap);
int nMaster = (int)soap_bind(&fun_soap, NULL, nPort, 100);
if (nMaster < 0)
{
soap_print_fault(&fun_soap, stderr);
exit(-1);
}
fprintf(stderr, "Socket connection successful : master socket = %d\n", nMaster);
while (true)
{
int nSlave = (int)soap_accept(&fun_soap);
if (nSlave < 0)
{
soap_print_fault(&fun_soap, stderr);
exit(-1);
}
fprintf(stderr, "Socket connection successful : slave socket = %d\n", nSlave);
soap_serve(&fun_soap);
soap_end(&fun_soap);
}
return 0;
}
Interface related code
take gservice.h Defined function implementation .
/* The concrete realization of addition */
int ns__add(struct soap* soap, double num1, double num2, double& result)
{
result = num1 + num2;
printf("[add] num1 = %lf, num2 = %lf, result = %lf\n", num1, num2, result);
return SOAP_OK;
}
/* The concrete implementation of subtraction */
int ns__sub(struct soap* soap, double num1, double num2, double& result)
{
result = num1 - num2;
printf("[sub] num1 = %lf, num2 = %lf, result = %lf\n", num1, num2, result);
return SOAP_OK;
}
/* The concrete realization of multiplication */
int ns__mult(struct soap* soap, double num1, double num2, double& result)
{
result = num1 * num2;
printf("[mul] num1 = %lf, num2 = %lf, result = %lf\n", num1, num2, result);
return SOAP_OK;
}
/* The concrete implementation of division */
int ns__divid(struct soap* soap, double num1, double num2, double& result)
{
result = num1 / num2;
printf("[div] num1 = %lf, num2 = %lf, result = %lf\n", num1, num2, result);
return SOAP_OK;
return SOAP_OK;
}
Compile operation
The following interface will appear , Click on “ allow access to ” that will do .
such , Our first one Web Service The server program has been running .
Access test
We can open the browser , Input localhost:8080 To make sure Web Service Whether it starts normally . We will see the following display .
In this way, our server has been completed .
At the same time, we can also see the connection information .
first Web Service client
The client can send application query to the server .
Build directory
We use gClient Catalog .
Build project
Or use VS 2019 Build a console application . This time our name is gClient.

Copy service files
take gservice.h Copy to the corresponding directory .
Generate wsdl file
Note that you need to use parameters here C, Indicates that it is a client .
soapcpp2.exe -C gservice.h
D:\gsoap-2.8\gsoap\gClient>soapcpp2.exe -C gservice.h
** The gSOAP code generator for C and C++, soapcpp2 release 2.8.119
** 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., [email protected]
** ----------------------------------------------------------------------------
Saving soapStub.h annotated copy of the source interface header file
Saving soapH.h serialization functions to #include in projects
Using ns service name: gservice
Using ns service style: rpc
Using ns service encoding: literal
Using ns schema namespace: http://tempuri.org/ns.xsd
Saving gservice.wsdl Web Service description
Saving gservice.add.req.xml sample SOAP/XML request
Saving gservice.add.res.xml sample SOAP/XML response
Saving gservice.sub.req.xml sample SOAP/XML request
Saving gservice.sub.res.xml sample SOAP/XML response
Saving gservice.mult.req.xml sample SOAP/XML request
Saving gservice.mult.res.xml sample SOAP/XML response
Saving gservice.divid.req.xml sample SOAP/XML request
Saving gservice.divid.res.xml sample SOAP/XML response
Saving gservice.nsmap namespace mapping table
Saving ns.xsd XML schema
Saving soapClient.cpp client call stub functions
Saving soapClientLib.cpp client stubs with serializers (use only for libs)
Saving soapC.cpp serialization functions
Compilation successful
Add necessary files to the project
take stdsoap2.h、soapStub.h、soapH.h、gservice.nsmap and stdsoap2.cpp、soapC.cpp、soapClient.cpp File import to gClient in , The project directory structure is as follows

modify gClient.cpp
increase soap Code
#include <stdio.h>
#include "../soapH.h"
#include "../gservice.nsmap"
int main(int argc, char* argv[])
{
printf("The Client is runing...\n");
double num1 = 110;
double num2 = 11;
double result = 0;
struct soap* CalculateSoap = soap_new();
//soap_init(CalculateSoap);
char server_addr[] = "http://localhost:8080";
int iRet = soap_call_ns__add(CalculateSoap, server_addr, "", num1, num2, result);
if (iRet == SOAP_ERR)
{
printf("Error while calling the soap_call_ns__add");
}
else
{
printf("Calling the soap_call_ns__add success.\n");
printf("%lf + %lf = %lf\n", num1, num2, result);
}
iRet = soap_call_ns__sub(CalculateSoap, server_addr, "", num1, num2, result);
if (iRet == SOAP_ERR)
{
printf("Error while calling the soap_call_ns__sub");
}
else
{
printf("Calling the soap_call_ns__sub success.\n");
printf("%lf - %lf = %lf\n", num1, num2, result);
}
iRet = soap_call_ns__mult(CalculateSoap, server_addr, "", num1, num2, result);
if (iRet == SOAP_ERR)
{
printf("Error while calling the soap_call_ns__mult");
}
else
{
printf("Calling the soap_call_ns__mult success.\n");
printf("%lf * %lf = %lf\n", num1, num2, result);
}
iRet = soap_call_ns__divid(CalculateSoap, server_addr, "", num1, num2, result);
if (iRet == SOAP_ERR)
{
printf("Error while calling the soap_call_ns__divid");
}
else
{
printf("Calling the soap_call_ns__divid success.\n");
printf("%lf / %lf = %lf\n", num1, num2, result);
}
soap_end(CalculateSoap);
soap_done(CalculateSoap);
free(CalculateSoap);
return 0;
}
Compile operation

Pictured above , We passed through Web Service The related calculation is realized .
边栏推荐
- jarvisoj_ level2
- Learning to use BACnet gateway of building control system is not so difficult
- 2、 What is the principle of layer 3 and 4 switching technology? Recommended collection!
- [DDCTF2018](╯°□°)╯︵ ┻━┻
- 【图像融合】基于伪 Wigner 分布 (PWD) 实现图像融合附matlab代码
- RDD basic knowledge points
- Win11 points how to divide disks? How to divide disks in win11 system?
- Global and Chinese market of bed former 2022-2028: Research Report on technology, participants, trends, market size and share
- 湖北专升本-湖师计科
- bjdctf_ 2020_ babystack
猜你喜欢

与(&&)逻辑或(||),动态绑定结合三目运算

【图像融合】基于伪 Wigner 分布 (PWD) 实现图像融合附matlab代码
![Selector (>, ~, +, [])](/img/7e/2becfcf7a7b2e743772deee5916caf.png)
Selector (>, ~, +, [])

【信号识别】基于深度学习CNN实现信号调制分类附matlab代码

Bjdctf 2020 Bar _ Babystack

阿里云全链路数据治理
![buuctf misc [UTCTF2020]docx](/img/e4/e160f704d6aa754e85056840e14bd2.png)
buuctf misc [UTCTF2020]docx

【图像融合】基于像素显着性结合小波变换实现多焦点和多光谱图像融合附matlab代码

The first common node of two linked lists_ The entry of the link in the linked list (Sword finger offer)
![[MRCTF2020]千层套路](/img/8e/d7b6e7025b87ea0f43a6123760a113.png)
[MRCTF2020]千层套路
随机推荐
Common coding and encryption in penetration testing
【Proteus】Arduino UNO + DS1307+LCD1602时间显示
Prefix and topic training
Global and Chinese market of water massage column 2022-2028: Research Report on technology, participants, trends, market size and share
A summary of the posture of bouncing and forwarding around the firewall
Deploy L2TP in VPN (medium)
[Proteus] Arduino uno + ds1307+lcd1602 time display
Virtual machine security disaster recovery construction
[MySQL usage Script] clone data tables, save query data to data tables, and create temporary tables
【WordPress建站】6. 文章内容防复制
Deploy loglistener in tke container to collect logs to CLS
buuctf misc [UTCTF2020]docx
How VPN works
More than 60 million shovel excrement officials, can they hold a spring of domestic staple food?
【图像融合】基于方向离散余弦变换和主成分分析的图像融合附matlab代码
Combine with (& &) logic or (||), dynamic binding and ternary operation
6000多万铲屎官,捧得出一个国产主粮的春天吗?
【MySQL 使用秘籍】克隆数据表、保存查询数据至数据表以及创建临时表
Tencent cloud security and privacy computing has passed the evaluation of the ICT Institute and obtained national recognition
相機標定(標定目的、原理)