C++通过gsoap发布WebServer和WebClient

C++通过gsoap发布WebServer和WebClient

gsoap C++ WebServer WebClient

转自
http://blog.youkuaiyun.com/u011720560/article/details/77603461

1.准备gsoap环境

gsoap下载地址

https://sourceforge.net/projects/gsoap2/files/latest/download?source=files
https://nchc.dl.sourceforge.net/project/gsoap2/gsoap-2.8/gsoap_2.8.52.zip

环境变量配置

将gsoap_2.8.52解压,放到C盘,然后将路径添加到环境变量,方便调用soapcpp2,
下面自己随便添加的,可以直接将 C:\gsoap\gsoap-2.8\gsoap\bin\win32 添加到用户PATH环境变量.

C:\gsoap\gsoap-2.8\gsoap\
\bin\win32

或者下面几条:

GSOAP_PATH C:\gsoap\gsoap-2.8\gsoap\
%GSOAP_PATH%\bin\win32
GSOAP_IMPORT %GSOAP_PATH%\import

备注:
C:\gsoap\gsoap-2.8\gsoap文件夹有stdsoap2.h和stdsoap2.cpp是后面CPP工程所需要的。

2.新建server.h文件

新建空文件夹,并在该文件夹下新建所需要的头文件server.h
添加下API接口内容:

    int ns__itoa(int i, char **a);
    int ns__add(int a, int b, int * result);

server.h 内容:

#ifndef _SERVER_H_
#define _SERVER_H_

//gsoap ns service name: itoa
//gsoap ns service protocol: SOAP
//gsoap ns service style: rpc
//gsoap ns service namespace: http://localhost:8087/itoa?wsdl 
//gsoap ns service location: http://localhost:8087
//gsoap ns service encoding: encoded  
//gsoap ns schema namespace: urn:itoa

int ns__itoa(int i, char **a);
int ns__add(int a, int b, int * result);

#endif

3.生成并准备一些所需要的文件

我是直接使用soapcpp2 server.h,即生成Server端也生成Client端

soapcpp2 server.h

下面三个都行

soapcpp2 -S server.h
soapcpp2 -S -I C:\gsoap\gsoap-2.8\gsoap\import server.h
soapcpp2 -S -I GSOAP_IMPORT server.h

-S 是指发布server端的 -C指发布Client端

将stdsoap2.h和stdsoap2.cpp文件复制到该路径下,后面会将所需要的文件复制到server工程或者client工程

4.创建测试工程

新建两个win32控制台的空工程,server端和client端的(本来想弄一个解决方案里面的,但是想想两个项目同时运行麻烦,就建立两个解决方案了)
然后添加一些所需要的文件到工程,工程没有进行其他配置,仅仅是添加一些对应的文件

Server:

这里面定义实现实现接口

server.cpp
server.h
server.h

下面的文件是gsoap生成的文

itoa.nsmap
soapC.cpp
soapH.h
soapServer.cpp
soapServerLib.cpp
soapStub.h
stdsoap2.cpp
stdsoap2.h

server.h 文件内容:

#include <iostream>
#include "server.h"

#include "soapH.h"
//#include <windows.h>
#include "itoa.nsmap"

int main()
{
    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;
}
// 服务消息的实现代码
int ns__itoa(struct soap *soap, int i, char **a) 
{ 
    *a = (char*)soap_malloc(soap, 11); 
    sprintf(*a, "%d", i); 
    return SOAP_OK; 
}
//server端的实现函数与itoa.h中声明的函数相同,但是多了一个当前的soap连接的参数
int ns__add(struct soap *soap, int a, int b, int * result)
{
    *result = a + b;
    return 0;
}

Client:

client.cpp
client.h

(下面的文件是gsoap生成的文件)

itoa.nsmap
soapC.cpp
soapClient.cpp
soapClientLib.cpp
soapH.h
soapStub.h
stdsoap2.cpp
stdsoap2.h

client.h 文件内容:

client.cpp 文件内容:

#include <iostream>
#include "soapH.h"
//#include <windows.h>
#include "itoa.nsmap"

using std::cout;
using std::cin;
using std::endl;

int TestAdd(const char* server)
{
    struct soap add_soap;
    int nResult = 0;
    soap_init(&add_soap);
    int nNum1 = 20;
    int nNum2 = 10;
    int nSum = 0;

    cout << "input two num : " << endl;
    cin >> nNum1 >> nNum2;

    soap_call_ns__add(&add_soap, server, "", nNum1, nNum2, &nSum);

    if (add_soap.error)
    {
        soap_print_fault(&add_soap, stderr);
    }
    soap_end(&add_soap);
    soap_done(&add_soap);
    return nSum;
}

int main()
{
    char* server = "http://127.0.0.1:8080";
    int result = TestAdd(server);
    cout << "TestAdd result=" << result << endl;

    system("pause");
    return 0;
}

运行结果

编译运行server端代码,然后编译运行client端代码

server:

Socket connection successful : master socket = 256
Socket connection successful : slave socket = 260

client:

input two num :
12
34
TestAdd result=46
请按任意键继续…

参考文档:

http://blog.youkuaiyun.com/ruanjwei1986/article/details/6938224
http://blog.youkuaiyun.com/testcs_dn/article/details/51463367

测试代码:

http://download.youkuaiyun.com/download/u011720560/9948003

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值