zoukankan      html  css  js  c++  java
  • gSOAP calc服务端与客户端示例

    1. Web服务定义描述头文件

    typedef double xsd__double;
    int ns__add(xsd__double a, xsd__double b, xsd__double &result);
    int ns__sub(xsd__double a, xsd__double b, xsd__double &result);
    int ns__sqrt(xsd__double a, xsd__double &result);

    2. 生成服务端与客户端代码

    soapcpp2 calc.h

    3. 服务端工程 / 客户端工程对比

    4. 服务端main.cpp

      1 #include "soapH.h"
      2 #include <math.h>
      3 
      4 
      5 int http_get(struct soap *soap)
      6 {
      7     FILE *fd = NULL;
      8     fd = fopen("ns.wsdl", "rb");
      9     if (!fd)
     10     {
     11         return 404;
     12     }
     13     soap->http_content = "text/xml";
     14     soap_response(soap, SOAP_FILE);
     15 
     16     while(true)
     17     {
     18         size_t r = fread(soap->tmpbuf, 1, sizeof(soap->tmpbuf), fd);
     19 
     20         if (!r)    break;
     21         if (soap_send_raw(soap, soap->tmpbuf, r)) break;
     22     }
     23     fclose(fd);
     24     soap_end_send(soap);
     25 
     26     return SOAP_OK;
     27 }
     28 
     29 int http_get1(struct soap *soap)
     30 {
     31     std::string fileName="ns.wsdl";
     32 
     33     FILE *fd = NULL;
     34     fd = fopen(fileName.c_str(), "rb");//openWSDLfiletocopy
     35     if(!fd)
     36     {
     37         return 404; //return HTTP not found error
     38     }
     39     soap->http_content="text/xml"; //HTTP header with text/xml content
     40     soap_response(soap,SOAP_FILE);
     41     while (true)
     42     {
     43         size_t r=fread(soap->tmpbuf,1,sizeof(soap->tmpbuf),fd);
     44         if(!r)    break;
     45         if(soap_send_raw(soap,soap->tmpbuf,r))    break; //cannot send, but little we can do about that
     46     }
     47     fclose(fd);
     48     soap_end_send(soap);
     49     return SOAP_OK;
     50 }
     51 
     52 
     53 
     54 int main()
     55 {
     56     struct soap soap;
     57     int m, s;
     58     soap_init(&soap);
     59     soap.fget = http_get;
     60 
     61     m = soap_bind(&soap, NULL, 9001, 100);
     62 
     63     if (m < 0)
     64     {
     65         soap_print_fault(&soap, stderr);
     66         exit(-1);
     67     }
     68 
     69         fprintf(stderr, "Socket connection successful: master socket = %d
    ", m);
     70 
     71         for (int i=1; ; i++)
     72         {
     73             s = soap_accept(&soap); //接受请求
     74             if (s < 0)
     75             {
     76                 soap_print_fault(&soap, stderr);
     77                 break;
     78             }
     79 
     80             fprintf(stderr, "%d: accepted connection from IP=%d.%d.%d.%d socket=%d", 
     81                         i, (soap.ip >> 24)&0xFF, (soap.ip >> 16)&0xFF, (soap.ip >> 8)&0xFF, soap.ip&&0xFF, s);  //打印出接受请求的详细信息
     82 
     83             if (soap_serve(&soap) != SOAP_OK)  //处理请求
     84                 soap_print_fault(&soap, stderr);
     85             fprintf(stderr, "request served
    ");
     86 
     87             soap_destroy(&soap); //清理soap类实例
     88             soap_end(&soap);      //清理soap相关的所有资源并关闭连接
     89     }
     90     soap_done(&soap);
     91 
     92     return soap_serve(soap_new());
     93 }
     94 
     95 int ns__add(struct soap *soap, double a, double b, double &result)
     96 {
     97     result = a + b;
     98     return SOAP_OK;
     99 }
    100 
    101 int ns__sub(struct soap *soap, double a, double b, double &result)
    102 {
    103     result = a - b;
    104     return SOAP_OK;
    105 }
    106 
    107 
    108 int ns__sqrt(struct soap *soap, double a, double &result)
    109 {
    110 
    111     if (a >= 0)
    112     {
    113         result = sqrt(a);
    114         return SOAP_OK;
    115     }
    116     else
    117         return soap_receiver_fault(soap, "Square root of negative number", "I can't only take the square root of a non-negative number");
    118 }
    119 
    120 
    121 
    122 
    123 SOAP_NMAC struct Namespace namespaces[] =
    124 {
    125     {"SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/", "http://www.w3.org/*/soap-envelope", NULL},
    126     {"SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/", "http://www.w3.org/*/soap-encoding", NULL},
    127     {"xsi", "http://www.w3.org/2001/XMLSchema-instance", "http://www.w3.org/*/XMLSchema-instance", NULL},
    128     {"xsd", "http://www.w3.org/2001/XMLSchema", "http://www.w3.org/*/XMLSchema", NULL},
    129     {"ns", "http://tempuri.org/ns.xsd", NULL, NULL},
    130     {NULL, NULL, NULL, NULL}
    131 };

    5. 客户端main.cpp

    #include "soapH.h"
    
    int main()
    {
        struct soap calc_soap;
        
    
        soap_init(&calc_soap);
    
        double result = -1;
    
        /* 
        soap_call_ns__add(
                struct soap *soap, 
                const char *soap_endpoint, 
                const char *soap_action, 
                double a, 
                double b, 
                double &result)
    
      */
        //调用成功返回标准0
        int flag = soap_call_ns__add(&calc_soap, "http://localhost:9001", "", 1.0, 6.9, result);
    
        std::cout<<"result="<<result<<", flag="<<flag<<std::endl;
    
    
    
        flag = soap_call_ns__sqrt(&calc_soap, "http://localhost:9001", "",  9, result);
        std::cout<<"result="<<result<<", flag="<<flag<<std::endl;
    
        return 0;
    };
    
    
    SOAP_NMAC struct Namespace namespaces[] =
    {
        {"SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/", "http://www.w3.org/*/soap-envelope", NULL},
        {"SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/", "http://www.w3.org/*/soap-encoding", NULL},
        {"xsi", "http://www.w3.org/2001/XMLSchema-instance", "http://www.w3.org/*/XMLSchema-instance", NULL},
        {"xsd", "http://www.w3.org/2001/XMLSchema", "http://www.w3.org/*/XMLSchema", NULL},
        {"ns", "http://tempuri.org/ns.xsd", NULL, NULL},
        {NULL, NULL, NULL, NULL}
    };

    6 服务端与客户端调试

  • 相关阅读:
    ffmpeg开发系列--av_read_frame()
    2013~2014
    javascript
    javascript
    javascript目录
    最长连续和与最大子矩阵
    成功者具备的十五种能力
    视频综合管理平台EasyNVS在实现代理时调用EasyDSS接口如何携带token?
    在线课堂Web无插件直播点播系统EasyDSS如何接入第三方视频管理平台EasyNVS?
    TSINGSEE青犀视频开发的EasyNTS上云网关网络穿透/远程运维在系统维护中的应用
  • 原文地址:https://www.cnblogs.com/asnjudy/p/4369108.html
Copyright © 2011-2022 走看看