zoukankan      html  css  js  c++  java
  • gsoap使用

    一. 安装gsoap

    下载地址:http://sourceforge.net/projects/gsoap2/files/

    解压安装:./configure --prefix=/usr/local/gsoap && make && make install

    示例目录:gsoap-2.8/gsoap/samples 有各类语言使用接口方法 

    async        chaining     gmt          Makefile                    mashup       README.txt  udp        xml-rpc-json
    atom         chaining++   googleapi    Makefile.am                 mashup++     rest        varparam
    autotest     curl         hello        Makefile.cpp_proxy_rules    mtom         roll        wcf
    aws          databinding  httpcookies  Makefile.cpp_proxy_rules_j  mtom-stream  router      webserver
    calc         dime         link         Makefile.cpp_rules          oneway       rss         wsa
    calc++       dom          link++       Makefile.c_rules            oneway++     ssl         wsrm
    calc_vs2005  factory      lu           Makefile.defines            polytest     template    wsse
    calc_xcode   factorytest  magic        Makefile.in                 primes       testmsgr    wst

    二. 编写demo

    定义函数接口,写chw.h文件

    //gsoap ns service name:    chw Simple calculator service
    //gsoap ns service style:   rpc
    //gsoap ns service encoding:    encoded
    //gsoap ns service namespace:   
    //gsoap ns service location:    
    
    //gsoap ns schema namespace:    urn:chw
    
    //gsoap ns service method-documentation: add Sums two values
    int add(int a, int b, int *result);
    
    //gsoap ns service method-documentation: sub Subtracts two values
    int sub(int a, int b, int *result);
    

    使用命令:soapcpp2 -c -x chw.h

    -c 产生纯C代码,否则是C++代码(与头文件有关)

    -I 指定import路径

    -x 不要产生XML示例文件

    生成文件有:

    [root@Logcen5 test]# ls
    chw.h    soapClient.c     soapH.h     soapServer.c     soapStub.h
    soapC.c  soapClientLib.c  soap.nsmap  soapServerLib.c

    查看soapStub.h文件

    /******************************************************************************
     *                                                                            *
     * Client-Side Call Stub Functions                                            *
     *                                                                            *
    ******************************************************************************/
    
    
        /** Web service synchronous operation 'soap_call_add' to the specified endpoint and SOAP Action header, returns SOAP_OK or error code */
        SOAP_FMAC5 int SOAP_FMAC6 soap_call_add(struct soap *soap, const char *soap_endpoint, const char *soap_action, int a, int b, int *result);
        /** Web service asynchronous operation 'soap_send_add' to send a request message to the specified endpoint and SOAP Action header, returns SOAP_OK or error code */
        SOAP_FMAC5 int SOAP_FMAC6 soap_send_add(struct soap *soap, const char *soap_endpoint, const char *soap_action, int a, int b);
        /** Web service asynchronous operation 'soap_recv_add' to receive a response message from the connected endpoint, returns SOAP_OK or error code */
        SOAP_FMAC5 int SOAP_FMAC6 soap_recv_add(struct soap *soap, int *result);
    
        /** Web service synchronous operation 'soap_call_sub' to the specified endpoint and SOAP Action header, returns SOAP_OK or error code */
        SOAP_FMAC5 int SOAP_FMAC6 soap_call_sub(struct soap *soap, const char *soap_endpoint, const char *soap_action, int a, int b, int *result);
        /** Web service asynchronous operation 'soap_send_sub' to send a request message to the specified endpoint and SOAP Action header, returns SOAP_OK or error code */
        SOAP_FMAC5 int SOAP_FMAC6 soap_send_sub(struct soap *soap, const char *soap_endpoint, const char *soap_action, int a, int b);
        /** Web service asynchronous operation 'soap_recv_sub' to receive a response message from the connected endpoint, returns SOAP_OK or error code */
        SOAP_FMAC5 int SOAP_FMAC6 soap_recv_sub(struct soap *soap, int *result);
    
    /******************************************************************************
     *                                                                            *
     * Server-Side Operations                                                     *
     *                                                                            *
    ******************************************************************************/
    
        /** Web service operation 'add' implementation, should return SOAP_OK or error code */
        SOAP_FMAC5 int SOAP_FMAC6 add(struct soap*, int a, int b, int *result);
        /** Web service operation 'sub' implementation, should return SOAP_OK or error code */
        SOAP_FMAC5 int SOAP_FMAC6 sub(struct soap*, int a, int b, int *result);

    另外需要从gsoap拷贝两个文件过来stdsoap2.c和stdsoap2.h,方便gcc编译不报错!

    编写自己的服务端。server.c

    #include <stdio.h>
    #include "soapH.h"
    #include "soap.nsmap"
    
    int main(int argc, char *argv[])
    {
        struct soap soap;
        SOAP_SOCKET m, s;
        int ret;
        soap_init(&soap);
        if (argc < 2)
            soap_serve(&soap);
        else
        {
            m = soap_bind(&soap, NULL, atoi(argv[1]), 100);
            if (!soap_valid_socket(m))
            {
                soap_print_fault(&soap, stderr);
                exit(-1);
            }
            fprintf(stderr, "Socket connection successful: master socket = %d
    ", m);
            for ( ; ; )
            {
                s = soap_accept(&soap);
                fprintf(stderr, "Socket connection successful: slave socket = %d
    ", s);
                if (!soap_valid_socket(s))
                {
                    soap_print_fault(&soap, stderr);
                    exit(-1);
                }
                soap_serve(&soap);
                soap_end(&soap);
            }
        }
        return 0;
    }
    
    int add(struct soap *soap, int a, int b, int *result)
    {
        *result = a + b;
        return SOAP_OK;
    }
    
    int sub(struct soap *soap, int a, int b, int *result)
    {
        *result = a - b;
        return SOAP_OK;
    }

    编译命令:gcc -o ser server.c soapC.c soapServer.c stdsoap2.c 生成可执行程序 ser

    编写自己的客户端。client.c

    #include "soapH.h"
    #include "soap.nsmap"
    
    const char server[] = "http://localhost:8888";
    
    int main(int argc, char *argv[])
    {
        struct soap soap;
        int a, b, result;
    
        if (argc < 4)
        {
            fprintf(stderr, "Usage: [add|sub|mul|div|pow] num num
    ");
            exit(0);
        }
        a = atoi(argv[2]);
        b = atoi(argv[3]);
    
        soap_init(&soap);
        switch(*argv[1])
        {
            case 'a':
                //soap_call_ns__add(&soap, server, "", a, b, &result);
                soap_call_add(&soap, server, "", a, b, &result);
                break;
            case 's':
                //soap_call_ns__sub(&soap, server, "", a, b, &result);
                soap_call_sub(&soap, server, "", a, b, &result);
                break;
            default:
                fprintf(stderr, "Unknown command
    ");
                exit(0);
        }
        if (soap.error)
        {
                soap_print_fault(&soap, stderr);
                exit(1);
        }
        else
                printf("result = %d
    ", result);
        soap_destroy(&soap);
        soap_end(&soap);
        soap_done(&soap);
    
        return 0;
    }
    

    编译命令:gcc -o cle client.c soapC.c soapClient.c stdsoap2.c生成可执行程序 cle

    测试:

    在一个窗口执行:./ser 8888

    [root@Logcen5 test]# ./ser 8888
    Socket connection successful: master socket = 3

    另外一个窗口执行:./cle add 2 3

    [root@Logcen5 test]# ./cle add 2 3
    result = 5
    
  • 相关阅读:
    PAIRING WORKFLOW MANAGER 1.0 WITH SHAREPOINT 2013
    Education resources from Microsoft
    upgrade to sql server 2012
    ULSViewer sharepoint 2013 log viewer
    Top 10 Most Valuable Microsoft SharePoint 2010 Books
    讨论 Setsockopt选项
    使用 Alchemy 技术编译 C 语言程序为 Flex 可调用的 SWC
    Nagle's algorithm
    Nagle算法 TCP_NODELAY和TCP_CORK
    Design issues Sending small data segments over TCP with Winsock
  • 原文地址:https://www.cnblogs.com/porkerface/p/11424686.html
Copyright © 2011-2022 走看看