当前位置:网站首页>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
 Insert picture description here

Add a path

take D:\gsoap-2.8\gsoap\bin\win64 Add to system environment variables . Here's the picture .
 Insert picture description here
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 .
 Insert picture description here
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 .
 Insert picture description here

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 .
 Insert picture description here

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 .
 Insert picture description here
 Insert picture description here
And I'm gonna go ahead and create . The generated blank project is shown in the following figure .
 Insert picture description here
 Insert picture description here

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 .
 Insert picture description here

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 .
 Insert picture description here
such , Our first one Web Service The server program has been running .
 Insert picture description here

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 .
 Insert picture description here
In this way, our server has been completed .
At the same time, we can also see the connection information .
 Insert picture description here

first Web Service client

The client can send application query to the server .

Build directory

We use gClient Catalog .
 Insert picture description here

Build project

Or use VS 2019 Build a console application . This time our name is gClient. Insert picture description here
 Insert picture description here

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

 Insert picture description here

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

 Insert picture description here

Pictured above , We passed through Web Service The related calculation is realized .

原网站

版权声明
本文为[Hard working Lao Zhou]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211619274420.html