vs2013 c++编的webservice,实现返回一个字符串功能

C++语言 码拜 8年前 (2016-04-06) 1285次浏览
安装例子做了一个webservice。例子里面是 做了一个add加法,返回了一个int类型的结果。
想返回一个字符串。求源代码。要源代码。
教程看了一堆了。
在服务器网址上看不到GetPointData函数。只有那个add函数。时间紧张,来不及研究,所以求帮助。
本人的.h文件

//gsoap ns service name: Service_MYJK
//gsoap ns service namespace: http://localhost/Service_MYJK.wsdl
//gsoap ns service location: http://localhost
//gsoap ns service executable: Service_MYJK.cgi
//gsoap ns service encoding: encoded
//gsoap ns schema namespace: urn:Service_MYJK
int ns__add(int num1, int num2, int* sum);
int GetPointData(int nMessage, int nArea, char* pcUser,char** pcRes);
int SetPointData(int nWtgID,  char* pcUser);

service的cpp文件
#include <stdio.h>
#include <stdlib.h>
#include “stdsoap2.h”
#include “soapH.h”
#include “Service_MYJK.nsmap”
#include “MainControlApi.h”
/////////////////////////////////////////////////////////////////////////
int   http_get(struct   soap   *soap);
int main(int argc, char* argv[])
{
int m, s;
struct soap add_soap;
soap_init(&add_soap);
//soap_set_namespaces(&add_soap, add_namespaces);
if (argc < 2)
{
printf(“usage: %s <server_port> \n”, argv[0]);
exit(1);
}
else
{
add_soap.fget = http_get;
m = soap_bind(&add_soap, NULL, atoi(argv[1]), 100);
if (m < 0)
{
soap_print_fault(&add_soap, stderr);
exit(-1);
}
fprintf(stderr, “Socket connection successful: master socket = %d\n”, m);
for (;;)
{
s = soap_accept(&add_soap);
if (s < 0)
{
soap_print_fault(&add_soap, stderr);
exit(-1);
}
fprintf(stderr, “Socket connection successful: slave socket = %d\n”, s);
soap_serve(&add_soap);//该句说明该server的服务
soap_end(&add_soap);
}
}
return 0;
}
//server端的实现函数与add.h中声明的函数相同,但是多了一个当前的soap连接的参数
int ns__add(struct soap *add_soap, int num1, int num2, int *sum)
{
*sum = num1 + num2;
return 0;
}
int GetPointData(struct soap *add_soap, int nMessage, int nArea, char* pcUser, char** pcRes)
{
sprintf_s(pcUser, nArea,”test result 1″);
sprintf_s(*pcRes, nArea, “test result 2”);
GetPointData(pcUser, nMessage, nArea);
return 0;
}
int SetPointData(struct soap *add_soap, int nWtgID, char* pcUser)
{
sprintf_s(pcUser, nWtgID, “SetPointData test result 2”);
return 0;
}
//http_get是一个函数

解决方案

40

testDemo.h

//gsoap ns service name: testDemo
//gsoap ns service namespace: http://localhost/testDemo.wsdl
//gsoap ns service location: http://localhost
//gsoap ns service executable: testDemo.cgi
//gsoap ns service encoding: encoded
//gsoap ns schema namespace: urn:add
typedef std::string xsd__string;
int ns__add( int num1, int num2, int* sum );
int ns__getStr( xsd__string src, xsd__string *result);

然后生成对应的文件
service端

#include <tchar.h>
#include <string>
#include "testDemo.h"
#include "testDemo.nsmap"
int _tmain(int argc, _TCHAR* argv[])
{
	int iSocket_master, iSocket_slaver; /**//* master and slave sockets */
	struct soap add_soap;
	soap_init(&add_soap);
	//soap_set_namespaces(&add_soap, add_namespaces);
	if (argc < 2)
	{
		wprintf(L"usage: %s <server_port> \n", argv[0]);
		exit(1);
	}
	else
	{
		//wprintf (L"%s\n", argv[1]);
		int port = _wtoi(argv[1]);
		iSocket_master = soap_bind(&add_soap, NULL, port, 100);
		if (iSocket_master < 0)
		{
			soap_print_fault(&add_soap, stderr);
			exit(-1);
		}

		fprintf(stderr, "Socket connection port=%d successful: master socket =%d\n",port, iSocket_master);
		for ( ; ; )
		{
			iSocket_slaver = soap_accept(&add_soap);
			if (iSocket_slaver < 0)
			{
				soap_print_fault(&add_soap, stderr);
				exit(-1);
			}
			fprintf(stderr, "Socket connection successful: slave socket= %d\n", iSocket_slaver);
			soap_serve(&add_soap);//该句说明该server的服务
			soap_end(&add_soap);
		}
	}
	return 0;
}
//server端的实现函数与testDemo.h中声明的函数相同,但是多了一个当前的soap连接的参数
/*加法的具体实现*/
int ns__add(struct soap *soap, int num1, int num2, int* result )   
{
	if (NULL == result)
	{
		printf("Error:The third argument should not be NULL!\n");
		return SOAP_ERR;
	}
	else
	{
		(*result) = num1 + num2;
		return SOAP_OK;
	}
	return SOAP_OK;
}
int ns__getStr(struct soap *soap, xsd__string src, xsd__string *result)   
{
	if (NULL == result)
	{
		printf("Error:The third argument should not be NULL!\n");
		return SOAP_ERR;
	}
	else
	{
		(*result) = std::string("hello ") + src;
		return SOAP_OK;
	}
	return SOAP_OK;
}

client端

#include <tchar.h>
#include <string>
#include "soapH.h"
#include "testDemo.nsmap"
int add(const char* server, int num1, int num2, int *sum);
int getStr(const char* server,std::string input, std::string *result);
int _tmain(int argc, _TCHAR* argv[])
{
	int result = -1;
	char* server="http://localhost:4567";
	int num1 = 0;
	int num2 = 0;
	int sum = 0;
	if( argc < 3 )
	{
		wprintf(L"usage: %s num1 num2 \n", argv[0]);
		exit(0);
	}
	num1 = _wtoi(argv[1]);
	num2 = _wtoi(argv[2]);
	result = add(server, num1, num2, &sum);
	if (result != 0)
	{
		printf("soap err,errcode = %d\n", result);
	}
	else
	{
		printf("%d+%d=%d\n", num1, num2, sum );
	}

	std::string input = "test";
	std::string strRet ;
	result = getStr(server, input, &strRet);
	if (result != 0)
	{
		printf("soap err,errcode = %d\n", result);
	}
	else
	{
		printf("%s,%s\n", input.c_str(), strRet.c_str());
	}
	return 0;
}
int add( const char* server, int num1, int num2, int *sum )
{
	struct soap add_soap;
	int result = 0;
	soap_init(&add_soap);
	//    soap_set_namespaces(&add_soap, add_namespaces);
	//该函数是客户端调用的主要函数,后面几个参数和add.h中声明的一样,前
	//面多了3个参数,函数名是接口函数名ns__add前面加上soap_call_
	soap_call_ns__add( &add_soap, server, "", num1, num2, sum );
	if(add_soap.error)
	{
		printf("soap error:%d,%s,%s/n", add_soap.error,
			*soap_faultcode(&add_soap), *soap_faultstring(&add_soap) );
		result = add_soap.error;
	}
	soap_end(&add_soap);
	soap_done(&add_soap);
	return result;
}
int getStr(const char* server,std::string input, std::string *resultStr)
{
	struct soap getStr_soap;
	int result = 0;
	soap_init(&getStr_soap);
	//    soap_set_namespaces(&add_soap, add_namespaces);
	soap_call_ns__getStr( &getStr_soap, server, "", input, resultStr);
	if(getStr_soap.error)
	{
		printf("soap error:%d,%s,%s/n", getStr_soap.error,
			*soap_faultcode(&getStr_soap), *soap_faultstring(&getStr_soap) );
		result = getStr_soap.error;
	}
	soap_end(&getStr_soap);
	soap_done(&getStr_soap);
	return result;
}

60

你本人的代码稍微改改就ok了。
你的服务器.h文件把函数改成这样
int ns__GetPointData(int nMessage, int nArea, char* pcUser,char** pcRes);
int ns__SetPointData(int nWtgID,  char* pcUser,char** pcRes);
char*是参数,char**放最后是返回值。
假如要返回多个,用个struct*
然后改你的服务器cpp文件
int ns__GetPointData(struct soap *add_soap, int nMessage, int nArea, char* pcUser, char** pcRes)
{
sprintf_s(pcUser, nArea,”test result 1″);
sprintf_s(*pcRes, nArea, “test result 2”);
GetPointData(pcUser, nMessage, nArea);
//不知道你这个GetPointData是什么
//用sprintf_s(*ret, strlen(res), “返回值”,);修改返回值,假如编码统一就能返回中文。
return 0;
}
int ns__SetPointData(struct soap *add_soap, int nWtgID, char* pcUser,char** pcRes)
{
sprintf_s(pcUser, nWtgID, “SetPointData test result 2”);
return 0;
}
把.h重新和exe放一起,用命令行编译。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明vs2013 c++编的webservice,实现返回一个字符串功能
喜欢 (0)
[1034331897@qq.com]
分享 (0)