gsoap入门实例

时间:2023-03-09 02:30:00
gsoap入门实例
  1. 环境
    VS2008,gsoap_2.8,win7
  2. 实例场景:在客户端输入一个字符串,然后传递给服务端计算字符串长度并返回给客户端(附加一些加减乘除法的实现);
  3. 将..\gsoap-2.8\gsoap\bin\win32中的两个exe文件所在路径加入环境变量中,后面有用;
  4. 新建一个文件夹,设计一个calculator.h文件,如下(前面几行的注释我也不知道有啥用)
     //gsoap ns service name: add
    //gsoap ns service namespace: http://localhost/add.wsdl
    //gsoap ns service location: http://localhost
    //gsoap ns service executable: add.cgi
    //gsoap ns service encoding: encoded
    //gsoap ns schema namespace: urn:add
    int ns__add(int num1, int num2, int* result );
    int ns__sub(int num1, int num2, int* result );
    int ns__mult( int num1, int num2, int *result);
    int ns__divid( int num1, int num2, int *result);
    int ns__Hello(char* str,int *len);
  5. 在该文件夹中打开cmd(方法ctrl+右键 --》在此处打开命令窗口),输入命令:soapcpp2.exe calculator.h
    有关gsoap的命令用法自行百度;
  6. 你会发现生成很多很多文件gsoap入门实例
  7. 利用VS2008创建一个calServer的工程,将calculator.h  add.nsmap soapC.cpp soapServer.cpp soapH.h soapStub.h放入该工程,并且将gsoap-2.8\gsoap下的stdsoap2.h stdsoap2.cpp也添加到该工程;添加完后,在项目属性中的链接器--输入--附加依赖项 中输入wsock32.lib;
  8. 在calServer中添加一个main.cpp,代码如下:
     #include "soapH.h"
    #include "add.nsmap"
    #include "stdio.h"
    #include <iostream>
    using namespace std; int main( int argc, char *argv[])
    {
    struct soap *CalculateSoap = soap_new(); //创建一个soap
    int iSocket_master = soap_bind(CalculateSoap, NULL, , ); //绑定到相应的IP地址和端口()NULL指本机,
    //10000为端口号,最后一个参数不重要。
    if (iSocket_master< ) //绑定出错
    {
    soap_print_fault(CalculateSoap, stderr);
    exit(-);
    }
    printf("SoapBind success,the master socket number is:%d\n",iSocket_master); //绑定成功返回监听套接字 while()
    {
    int iSocket_slaver = soap_accept(CalculateSoap);
    if (iSocket_slaver < )
    {
    soap_print_fault(CalculateSoap, stderr);
    exit(-);
    }
    printf("Get a new connection,the slaver socket number is:%d\n",iSocket_slaver); //绑定成功返回监听套接字
    soap_serve(CalculateSoap);
    soap_end(CalculateSoap);
    }
    soap_done(CalculateSoap);
    free(CalculateSoap); return ;
    } /*加法的具体实现*/
    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__sub(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__mult(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__divid(struct soap *soap, int num1, int num2, int *result)
    {
    if (NULL == result || == num2)
    {
    printf("Error:The second argument is 0 or The third argument is NULL!\n");
    return SOAP_ERR;
    }
    else
    {
    (*result) = num1 / num2;
    return SOAP_OK;
    }
    return SOAP_OK;
    } int ns__Hello(struct soap *soap, char *str, int *len)
    {
    /*if (NULL == result)
    {
    printf("Error:The third argument should not be NULL!\n");
    return SOAP_ERR;
    }
    else
    {
    cout << result <<endl;
    return SOAP_OK;
    }*/
    //if (NULL == result)
    cout << str <<endl;
    (*len) = strlen(str);
    return SOAP_OK; }
  9. Server端完成,可以运行了。
  10. 客户端:利用VS2008创建一个calClient的工程,将calculator.h  add.nsmap soapC.cpp soapClient.cpp soapH.h soapStub.h放入该工程,并且将gsoap-2.8\gsoap下的stdsoap2.h stdsoap2.cpp也添加到该工程;添加完后,在项目属性中的链接器--输入--附加依赖项 中输入wsock32.lib;
  11. 在calClient中添加一个main.cpp,代码如下:
     #include "soapH.h"
    #include "add.nsmap"
    #include "stdio.h"
    #include <iostream>
    using namespace std; int main( int argc, char *argv[])
    {
    printf("The Client is runing...\n");
    int num1 = ;
    int num2 = ;
    int result = ; struct soap *CalculateSoap = soap_new();
    //soap_init(CalculateSoap);
    注意改你的ip,端口10000不改
    char * server_addr = "http://xx.x.x.x:10000"; 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("%d + %d = %d\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("%d - %d = %d\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("%d * %d = %d\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("%d / %d = %d\n",num1,num2,result);
    }
    char *str = new char[];
    cin.getline(str,);
    cout << str <<endl;
    int len;
    iRet = soap_call_ns__Hello(CalculateSoap,server_addr,"",str,&len);
    if ( iRet == SOAP_ERR)
    {
    printf("Error while calling the soap_call_ns__add");
    }
    else
    {
    cout << str << " length is " << len <<" success!\n";
    } soap_end(CalculateSoap);
    soap_done(CalculateSoap);
    free(CalculateSoap); return ;
    }
  12. Client端完成,可以先运行Server,在运行Client看看效果。
    注意:客户端和服务端可以再两台电脑上允许并访问。