• gsoap入门实例


    1. 环境
      VS2008,gsoap_2.8,win7
    2. 实例场景:在客户端输入一个字符串,然后传递给服务端计算字符串长度并返回给客户端(附加一些加减乘除法的实现);
    3. 将..gsoap-2.8gsoapinwin32中的两个exe文件所在路径加入环境变量中,后面有用;
    4. 新建一个文件夹,设计一个calculator.h文件,如下(前面几行的注释我也不知道有啥用)
       1 //gsoap ns service name: add
       2 //gsoap ns service namespace: http://localhost/add.wsdl
       3 //gsoap ns service location: http://localhost
       4 //gsoap ns service executable: add.cgi
       5 //gsoap ns service encoding: encoded
       6 //gsoap ns schema namespace: urn:add
       7 int ns__add(int num1, int num2, int* result );
       8 int ns__sub(int num1, int num2, int* result );
       9 int ns__mult( int num1, int num2, int *result);
      10 int ns__divid( int num1, int num2, int *result);
      11 int ns__Hello(char* str,int *len);
    5. 在该文件夹中打开cmd(方法ctrl+右键 --》在此处打开命令窗口),输入命令:soapcpp2.exe calculator.h
      有关gsoap的命令用法自行百度;
    6. 你会发现生成很多很多文件
    7. 利用VS2008创建一个calServer的工程,将calculator.h  add.nsmap soapC.cpp soapServer.cpp soapH.h soapStub.h放入该工程,并且将gsoap-2.8gsoap下的stdsoap2.h stdsoap2.cpp也添加到该工程;添加完后,在项目属性中的链接器--输入--附加依赖项 中输入wsock32.lib;
    8. 在calServer中添加一个main.cpp,代码如下:
        1 #include "soapH.h"
        2 #include "add.nsmap"
        3 #include "stdio.h"
        4 #include <iostream>
        5 using namespace std;
        6 
        7 int main( int argc, char *argv[])
        8 {
        9     struct soap *CalculateSoap = soap_new();                                //创建一个soap
       10     int iSocket_master = soap_bind(CalculateSoap, NULL, 10000, 100);         //绑定到相应的IP地址和端口()NULL指本机,
       11     //10000为端口号,最后一个参数不重要。
       12     if (iSocket_master< 0)                                                  //绑定出错
       13     {
       14         soap_print_fault(CalculateSoap, stderr);
       15         exit(-1);
       16     }
       17     printf("SoapBind success,the master socket number is:%d
      ",iSocket_master); //绑定成功返回监听套接字
       18 
       19     while(1)
       20     {
       21         int iSocket_slaver = soap_accept(CalculateSoap);
       22         if (iSocket_slaver < 0)
       23         {
       24             soap_print_fault(CalculateSoap, stderr);
       25             exit(-2);
       26         }
       27         printf("Get a new connection,the slaver socket number is:%d
      ",iSocket_slaver); //绑定成功返回监听套接字
       28         soap_serve(CalculateSoap);
       29         soap_end(CalculateSoap);
       30     }
       31     soap_done(CalculateSoap);
       32     free(CalculateSoap);
       33 
       34     return 0;
       35 }
       36 
       37 /*加法的具体实现*/
       38 int ns__add(struct soap *soap, int num1, int num2, int* result )   
       39 {
       40     if (NULL == result)
       41     {
       42         printf("Error:The third argument should not be NULL!
      ");
       43         return SOAP_ERR;
       44     }
       45     else
       46     {
       47         (*result) = num1 + num2;
       48         return SOAP_OK;
       49     }
       50     return SOAP_OK;
       51 }
       52 
       53 /*减法的具体实现*/
       54 int ns__sub(struct soap *soap,int num1, int num2, int* result )
       55 {
       56     if (NULL == result)
       57     {
       58         printf("Error:The third argument should not be NULL!
      ");
       59         return SOAP_ERR;
       60     }
       61     else
       62     {
       63         (*result) = num1 - num2;
       64         return SOAP_OK;
       65     }
       66     return SOAP_OK;
       67 }
       68 
       69 /*乘法的具体实现*/
       70 int ns__mult(struct soap *soap, int num1, int num2, int *result)
       71 {
       72     if (NULL == result)
       73     {
       74         printf("Error:The third argument should not be NULL!
      ");
       75         return SOAP_ERR;
       76     }
       77     else
       78     {
       79         (*result) = num1 * num2;
       80         return SOAP_OK;
       81     }
       82     return SOAP_OK;
       83 }
       84 
       85 /*除法的具体实现*/
       86 int ns__divid(struct soap *soap, int num1, int num2, int *result)
       87 {
       88     if (NULL == result || 0 == num2)
       89     {
       90         printf("Error:The second argument is 0 or The third argument is NULL!
      ");
       91         return SOAP_ERR;
       92     }
       93     else
       94     {
       95         (*result) = num1 / num2;
       96         return SOAP_OK;
       97     }
       98     return SOAP_OK;
       99 }
      100 
      101 int ns__Hello(struct soap *soap, char *str, int *len)
      102 {
      103     /*if (NULL == result)
      104     {
      105         printf("Error:The third argument should not be NULL!
      ");
      106         return SOAP_ERR;
      107     }
      108     else
      109     {
      110         cout << result <<endl;
      111         return SOAP_OK;
      112     }*/
      113     //if (NULL == result)
      114     cout << str <<endl;
      115     (*len) = strlen(str);
      116     return SOAP_OK;
      117 
      118 }
    9. Server端完成,可以运行了。
    10. 客户端:利用VS2008创建一个calClient的工程,将calculator.h  add.nsmap soapC.cpp soapClient.cpp soapH.h soapStub.h放入该工程,并且将gsoap-2.8gsoap下的stdsoap2.h stdsoap2.cpp也添加到该工程;添加完后,在项目属性中的链接器--输入--附加依赖项 中输入wsock32.lib;
    11. 在calClient中添加一个main.cpp,代码如下:
       1 #include "soapH.h"
       2 #include "add.nsmap"
       3 #include "stdio.h"
       4 #include <iostream>
       5 using namespace std;
       6 
       7 int main( int argc, char *argv[])
       8 {
       9     printf("The Client is runing...
      ");
      10     int num1 = 110;
      11     int num2 = 11;
      12     int result = 0;
      13 
      14     struct soap *CalculateSoap = soap_new();
      15     //soap_init(CalculateSoap);
      16        注意改你的ip,端口10000不改
      17     char * server_addr = "http://xx.x.x.x:10000";
      18 
      19     int iRet = soap_call_ns__add(CalculateSoap,server_addr,"",num1,num2,&result);
      20     if ( iRet == SOAP_ERR)
      21     {
      22         printf("Error while calling the soap_call_ns__add");
      23     }
      24     else
      25     {
      26         printf("Calling the soap_call_ns__add success。
      ");
      27         printf("%d + %d = %d
      ",num1,num2,result);
      28     }
      29 
      30     iRet = soap_call_ns__sub(CalculateSoap,server_addr,"",num1,num2,&result);
      31     if ( iRet == SOAP_ERR)
      32     {
      33         printf("Error while calling the soap_call_ns__sub");
      34     }
      35     else
      36     {
      37         printf("Calling the soap_call_ns__sub success。
      ");
      38         printf("%d - %d = %d
      ",num1,num2,result);
      39     }
      40 
      41     iRet = soap_call_ns__mult(CalculateSoap,server_addr,"",num1,num2,&result);
      42     if ( iRet == SOAP_ERR)
      43     {
      44         printf("Error while calling the soap_call_ns__mult");
      45     }
      46     else
      47     {
      48         printf("Calling the soap_call_ns__mult success。
      ");
      49         printf("%d * %d = %d
      ",num1,num2,result);
      50     }
      51 
      52     iRet = soap_call_ns__divid(CalculateSoap,server_addr,"",num1,num2,&result);
      53     if ( iRet == SOAP_ERR)
      54     {
      55         printf("Error while calling the soap_call_ns__divid");
      56     }
      57     else
      58     {
      59         printf("Calling the soap_call_ns__divid success。
      ");
      60         printf("%d / %d = %d
      ",num1,num2,result);
      61     }
      62     char *str = new char[1024];
      63     cin.getline(str,1024);
      64     cout << str <<endl;
      65     int len;
      66     iRet = soap_call_ns__Hello(CalculateSoap,server_addr,"",str,&len);
      67     if ( iRet == SOAP_ERR)
      68     {
      69         printf("Error while calling the soap_call_ns__add");
      70     }
      71     else
      72     {
      73         cout << str << " length is " << len <<" success!
      ";
      74     }
      75 
      76     soap_end(CalculateSoap);
      77     soap_done(CalculateSoap);
      78     free(CalculateSoap);
      79 
      80     return 0;
      81 }
    12. Client端完成,可以先运行Server,在运行Client看看效果。
      注意:客户端和服务端可以再两台电脑上允许并访问。
  • 相关阅读:
    LeetCode链表解题模板
    c++中的new、operator new、placement new
    树的前序、中序、后续、层次遍历的递归和非递归解法
    c++Volatile关键词
    南大算法设计与分析课程OJ答案代码(5)--割点与桥和任务调度问题
    c++右值引用以及使用
    c++选择重载函数
    从4行代码看右值引用
    被遗忘的C结构体打包技术
    南大算法设计与分析课程OJ答案代码(4)--变位词、三数之和
  • 原文地址:https://www.cnblogs.com/LCCRNblog/p/3854172.html
Copyright © 2020-2023  润新知