gsoap 安装以及使用

1.安装
    unzip gsoap_2.8.1.zip
    ./configure
    make 
    make install
    
    cd /usr/local
    find . -name "*gsoap*"




2.编译出头文件
    C++方式(使用STL)
    wsdl2h -o calc.h http://www.genivia.com/calc.wsdl 
    
    C++方式(不使用STL)
    wsdl2h -s -o calc.h http://www.genivia.com/calc.wsdl 
    
    C方式
    wsdl2h -c -o calc.h http://www.genivia.com/calc.wsdl 


    选项说明
    -o 文件名,指定输出头文件 
    -n 名空间前缀 代替默认的ns 
    -c 产生纯C代码,否则是C++代码 
    -s 不要使用STL代码 
    -t 文件名,指定type map文件,默认为typemap.dat 
    -e 禁止为enum成员加上名空间前缀 
3.编译出stub   
    soapcpp2 -i -C -I/usr/local/share/gsoap/import calc.h 
    
    选项说明
    -C 仅生成客户端代码 
    -S 仅生成服务器端代码 
    -L 不要产生soapClientLib.c和soapServerLib.c文件 
    -c 产生纯C代码,否则是C++代码(与头文件有关) 
    -I 指定import路径(见上文) 
    -x 不要产生XML示例文件 
    -i 生成C++包装,客户端为xxxxProxy.h(.cpp),服务器端为xxxxService.h(.cpp)。
    
4.生成文件查看
    calc.add.req.xml  calc.h            calc.pow.req.xml  soapC.cpp          soapcalcProxy.h
    calc.add.res.xml  calc.mul.req.xml  calc.pow.res.xml  soapH.h            test.bak
    calc.div.req.xml  calc.mul.res.xml  calc.sub.req.xml  soapStub.h         test.cpp
    calc.div.res.xml  calc.nsmap        calc.sub.res.xml  soapcalcProxy.cpp
    
    test.cpp 
    ---------------------------------------------------------------------------------------
    #include "soapcalcProxy.h" // get proxy 
    #include "calc.nsmap" // import the generated namespace mapping table 
    int main()
    {
        calcProxy calc(SOAP_XML_INDENT);
        double sum;
        if (calc.add(1.0, 2.0, sum) == SOAP_OK)
          std::cout << "Sum = " << sum << std::endl;
        else
          calc.soap_stream_fault(std::cerr);
        return calc.error; // nonzero when error 
    }
    ---------------------------------------------------------------------------------------
5.编译代码
    g++ -o ta test.cpp  soapcalcProxy.cpp  soapC.cpp -lgsoap++ 
    




6.服务端
    currentTime.h 
    ---------------------------------------------------------------------------------------
    //File: currentTime.h 
    //gsoap ns service name: currentTime 
    //gsoap ns service namespace: urn:currentTime 
    //gsoap ns service location: http://www.yourdomain.com/currentTime.cgi 
    int ns__currentTime(time_t& response); 
    ---------------------------------------------------------------------------------------
    currentTime.cpp
    ---------------------------------------------------------------------------------------
    // File: currentTime.cpp 
    #include "soapcurrentTimeService.h" // include the proxy declarations 
    #include "currentTime.nsmap" // include the XML namespace mappings 
    int main() 
    { 
       // create server and serve one CGI-based request: 
       currentTimeService server; 
       return server.serve(); 
    } 
    int currentTimeService::currentTime(time_t& response) 
    { 
       response = time(0); 
       return SOAP_OK; 
    } 
    ---------------------------------------------------------------------------------------
    soapcpp2 -i -S currentTime.h     
    g++ -o currentTime.cgi currentTime.cpp soapC.cpp soapcurrentTimeService.cpp -lgsoap++ 
    
    
    soapcpp2 -i -C -I/usr/local/share/gsoap/import currentTime.h 


7.模拟测试
    编写add.h (必须添加前面的描述)
    
    //File: add.h 
    //gsoap ns service name: Add
    //gsoap ns service namespace: urn:Add 
    //gsoap ns service location: http://localhost:9000
    int ns_add(int a,int b, int *piRes);
    
    生成中间文件
    soapcpp2 -i -S add.h
    
    编写服务端代码 add.cpp
    ----------------------------------------------------------------
    #include "soapH.h"  //必须包含
    #include "Add.nsmap" //必须包含
    #include "soapAddService.h" //必须包含
    
    #include <iostream>
    using namespace std;
    //函数的具体实现
    int AddService::add(int num1, int num2, int *sum)
    {
        *sum = num1 + num2 + 1;
        return 0;
    }
    
    int main(int argc ,char *argv[])
    {
        printf("test begin...\n");
        
        AddService ads;
        
        //serve as CGI application
        if(argc < 2)
        {
            ads.serve();
        }
        else
        {
            int port = atoi(argv[1]);
            
            if(ads.run(port))
            {
                ads.soap_stream_fault(std::cerr);
                return -1;
                
            }
        }
        
        return 0;
    }
    //CGI方式
    g++ -o add.cgi soapC.cpp soapAddService.cpp add.cpp -lgsoap++ 
    ----------------------------------------------------------------
    
    生成客户端文件
    soapcpp2 -i -C add.h
    
    编写客户端端代码 addc.cpp
    ----------------------------------------------------------------      
    #include "soapH.h"  //必须包含
    #include "Add.nsmap" //必须包含
    #include "soapAddProxy.h" //必须包含
    
    #include <iostream>
    using namespace std;
    
    int main(int argc ,char *argv[])
    {
        int result = -1;
        char* server="http://localhost:9000";
        
        int num1 = 1;
        int num2 = 3;
        int sum = 0;
      
        AddProxy  ads;
        result = ads.add(num1, num2, &sum);
        if (result != 0)
        {
            printf("soap err, errcode = %d \n", result);
            printf("errmsg = [%s]\n",ads.soap_fault_detail());
            
        }
        else
        {
            printf("%d + %d = %d \n", num1, num2, sum);
        }
        
        return 0;
    }


    ----------------------------------------------------------------      
    
    
    --------------------------Makefile------------------------------    
    SOAP_INCL = -I/usr/local/share/gsoap/import
    OBJS = soapC.o soapAddService.o add.o
    OBJSC = soapC.o soapAddProxy.o addc.o
    all:adds addc
    adds:$(OBJS)
    g++ -o $@ $(OBJS) $(SOAP_INCL) -lgsoap++
    addc:$(OBJSC)
    g++ -o $@ $(OBJSC) $(SOAP_INCL) -lgsoap++
    %.o:%.cpp
    g++ -c $< -o $@ 
    clean:
    rm -rf *.o adds addc


    --------------------------Makefile------------------------------    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值