c语言初探gsoap



1 下载gsoap并编译安装

1.1 下载gsoap

下载地址:
http://sourceforge.net/projects/gsoap2
官方地址:
http://genivia.com/Products/gsoap/index.html

1.2 安装gsoap

unzip gsoap_2.8.120.zip
cd gsoap-2.8
./configure --prefix=/usr/local/gsoap
make && make install
# 编译过程中遇到的缺少相关库的问题,通过yum进行安装

2 初探

2.1 add.h以生成相关文件

  • 创建add.h文件,目的是生成gsoap相关的文件,add.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* sum );

  • 通过soapcpp2工具生成:
soapcpp2 -c add.h
-c: 生成c语言相关文件
  • 查看生成的文件,如下:
 ll
total 140K
-rw-r--r-- 1 root root  430 Apr  4 06:23 add.add.req.xml
-rw-r--r-- 1 root root  426 Apr  4 06:23 add.add.res.xml
-rw-r--r-- 1 root root  289 Apr  3 15:53 add.h
-rw-r--r-- 1 root root  656 Apr  4 06:23 add.nsmap
-rw-r--r-- 1 root root 2.9K Apr  4 06:23 add.wsdl
-rw-r--r-- 1 root root 1.2K Apr  4 06:23 ns.xsd
-rw-r--r-- 1 root root  50K Apr  4 06:23 soapC.c
-rw-r--r-- 1 root root 3.1K Apr  4 06:23 soapClient.c
-rw-r--r-- 1 root root 1.3K Apr  4 06:23 soapClientLib.c
-rw-r--r-- 1 root root  34K Apr  4 06:23 soapH.h
-rw-r--r-- 1 root root 3.4K Apr  4 06:23 soapServer.c
-rw-r--r-- 1 root root 1.3K Apr  4 06:23 soapServerLib.c
-rw-r--r-- 1 root root  12K Apr  4 06:23 soapStub.h

2.2 编写服务端

  • 编写addserver.c文件
#include "soapH.h"
#include "add.nsmap"

int main(int argc, char* argv[])
{
    int m, s; /* master and slave sockets */
    struct soap add_soap;
    soap_init(&add_soap);
    //soap_set_namespaces(&add_soap, add_namespaces);
    if (argc < 2)
    {
        printf("usage: %s <server_port> \n", argv[0]);
        exit(1);
    }
    else
    { 
        m = soap_bind(&add_soap, NULL, atoi(argv[1]), 100);
        if (m < 0)
        {
            soap_print_fault(&add_soap, stderr);
            exit(-1);
        }
        fprintf(stderr, "Socket connection successful: master socket = %d\n", m);
        for ( ; ; )
        { 
            s = soap_accept(&add_soap); 
            if (s < 0)
            { 
                soap_print_fault(&add_soap, stderr);
                exit(-1);
            }
            fprintf(stderr, "Socket connection successful: slave socket = %d\n", s);
            soap_serve(&add_soap);//该句说明该server的服务
            soap_end(&add_soap);
        }
    }
    return 0;
}
//server端的实现函数与add.h中声明的函数相同,但是多了一个当前的soap连接的参数
int ns__add(struct soap *add_soap, int num1, int num2, int *sum)
{
    *sum = num1 + num2;
    return 0;
}

  • 编译服务端
gcc -o server addserver.c stdsoap2.c soapC.c soapServer.c -I/usr/local/gsoap/include -lm -g
  • 运行服务器
./server 8888

界面访问结果:
localhost:8888
server端运行结果
说明服务端运行成功

2.3 编写客户端测试

  • 编写addcient.c
//#include "addStub.h"
//#include "soapStub.h"
#include "soapH.h"
#include "add.nsmap"

/**

* 传入参数:server:server的地址

* num1,num2:需要相加的数

* 传出参数:sum:num1和num2相加的结果

* 返回值:0为成功,其他为失败

*/

int add( const char* server, int num1, int num2, int *sum )
{
    struct soap add_soap;

    int result = 0;

    soap_init(&add_soap);

    // 该函数是客户端调用的主要函数,后面几个参数和add.h中声明的一样,前面多了3个参数,

    // 函数名是接口函数名ns__add前面加上soap_call_

    soap_call_ns__add( &add_soap, server, "", num1, num2, sum );

    if(add_soap.error)
    {
        printf("soap error:%d,%s,%s ",

        add_soap.error,

        *soap_faultcode(&add_soap),

        *soap_faultstring(&add_soap) );

        result = add_soap.error;

    }

    soap_end(&add_soap);

    soap_done(&add_soap);

    return result;

}

int main(int argc, char **argv)

{
    int result = -1;

    char* server="http://127.0.0.1:8888";

    int num1 = 0;

    int num2 = 0;

    int sum = 0;

    if( argc < 3 ){
        printf("usage: %s num1 num2 ", argv[0]);

        exit(0);
    }

    num1 = atoi(argv[1]);

    num2 = atoi(argv[2]);

    result = add(server, num1, num2, &sum);

    if (result != 0){
        printf("soap err,errcode = %d ", result);

    }else{
        printf("%d+%d=%d ", num1, num2, sum );
    }

    return 0;

}

  • 编译客户端
gcc -o client addclient.c stdsoap2.c soapC.c soapClient.c -I/usr/local/gsoap/include -lm
  • 运行客户端
./client 7 8
  • 查看运行结果
7+8=15 #

总结

  • 服务端和客户端引用的文件均是通过soapcpp2生成的,在引用上不需要包含源文件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值