Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2092702
  • 博文数量: 229
  • 博客积分: 7217
  • 博客等级: 上校
  • 技术积分: 3224
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-19 17:23
个人简介

个人主页https://xugaoxiang.com,微信公众号: Dev_Club 或者搜索 程序员Club

文章分类

全部博文(229)

文章存档

2017年(1)

2016年(20)

2015年(23)

2013年(1)

2012年(23)

2011年(68)

2010年(62)

2009年(31)

分类: LINUX

2012-06-15 16:40:38

1、Web Service
    Web Service就是为应用程序提供的一个网络接口。客户端发送数据,服务端获取数据并进行处理,然后将结果返回给客户端,从而达到交互和分布式处理的效果,与平台、与开发语言无关。

2、WSDL文件
    WSDL文件提供了一种对函数、函数参数及返回值描述的标准的xml文件.WSDL简化了开发流程,我们只要从服务提供商处获得相应接口的wsdl文件,通过该文件,利用软件自动生成客户端不同语言的代码。

3、搭建gSOAP环境
    3.1 下载地址:
        
    3.2 gSOAP环境搭建
         unzip gsoap_2.8.9.zip
         cd gsoap-2.8/src
         make -f MakefileManual(生成soapcpp2)

         cd gsoap-2.8/wsdl
         make -f MakefileManual(生成wsdl2h)

4、示例程序
    这里借用互联网上的一个例子。客户端输入两个数字,服务端提供加法函数服务并将结果返回给客户端。因为这个例子相对简单,就不用wsdl文件,而是直接从头文件开始生成代码。

    4.1 创建工作目录
        mkdir gSoapTest

    4.2 拷贝所需文件
        将gsoap源码包中的stdsoap2.c、stdsoap2.h及3.2中产生的soapcpp2拷贝到工作目录,如果有wsdl文件,则swdl2h也需要拷贝过来。

    4.3 定义函数声明文件add.h,定义函数接口

点击(此处)折叠或打开

  1. int ns2__add( int num1, int num2, int* sum );
   
    4.4 调用soapcpp2生成远程调用需要的文件

        soapcpp2 -c add.h

    4.5 创建服务端程序addserver.c

点击(此处)折叠或打开

  1. #include "soapH.h"
  2. #include "add.nsmap"

  3. int main(int argc, char **argv) {
  4.     int m, s;
  5.     struct soap add_soap;
  6.     soap_init(&add_soap);
  7.     soap_set_namespaces(&add_soap, namespaces);
  8.     if (argc < 2) {
  9.         printf("usage: %s \n", argv[0]);
  10.          exit(1);
  11.     } else {
  12.         m = soap_bind(&add_soap, NULL, atoi(argv[1]), 100);
  13.         if (m < 0) {
  14.             soap_print_fault(&add_soap, stderr);
  15.              exit(-1);
  16.         }
  17.         fprintf(stderr, "Socket connection successful: master socket = %d\n", m);
  18.         for (;;) {
  19.             s = soap_accept(&add_soap);
  20.             if (s < 0) {
  21.                 soap_print_fault(&add_soap, stderr);
  22.                 exit(-1);
  23.             }
  24.             fprintf(stderr, "Socket connection successful: slave socket = %d\n", s);
  25.             soap_serve(&add_soap);
  26.             soap_end(&add_soap);
  27.         }
  28.     }
  29.     return 0;
  30. }

  31. int ns2__add(struct soap *add_soap, int num1, int num2, int *sum) {
  32.     *sum = num1 + num2;
  33.     return 0;
  34. }
    4.6 创建客户端程序addclient.c

点击(此处)折叠或打开

  1. #include "soapStub.h"
  2. #include "add.nsmap"

  3. int add(const char *server, int num1, int num2, int *sum) {
  4.     struct soap add_soap;
  5.     int result = 0;
  6.     soap_init(&add_soap);
  7.     soap_set_namespaces(&add_soap, namespaces);
  8.     soap_call_ns2__add(&add_soap, server, NULL, num1, num2, sum);
  9.     printf("server is %s, num1 is %d, num2 is %d\n", server, num1, num2);
  10.     if (add_soap.error) {
  11.         printf("soap error: %d, %s, %s\n", add_soap.error, *soap_faultcode(&add_soap), *soap_faultstring(&add_soap));
  12.         result = add_soap.error;
  13.     }
  14.     soap_end(&add_soap);
  15.     soap_done(&add_soap);
  16.     return result;
  17. }
    4.7 创建测试程序addtest.c 

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. int add(const char *server, int num1, int num2, int *sum);

  5. int main(int argc, char **argv) {
  6.     int result = -1;
  7.     char server[128] = {0};
  8.     int num1;
  9.     int num2;
  10.     int sum;

  11.     if (argc < 4) {
  12.         printf("usage: %s num1 num2 \n", argv[0]);
  13.         exit(1);
  14.     }

  15.     strcpy(server,argv[1]);
  16.     num1 = atoi(argv[2]);
  17.     num2 = atoi(argv[3]);
  18.     result = add(server, num1, num2, &sum);
  19.    
  20.     if (result != 0) {
  21.         printf("soap error, errcode=%d\n", result);
  22.     } else {
  23.          printf("%d + %d = %d\n", num1, num2, sum);
  24.     }
  25.     return 0;
  26. }
    4.8 编译程序
        gcc -o addserver addserver.c soapH.h stdsoap2.h stdsoap2.c soapServer.c soapC.c
        gcc -o addclient addclient.c soapH.h stdsoap2.h stdsoap2.c soapClient.c soapC.c addtest.c

    4.9 运行程序
        Server端:  ./addserver 7788
        Client端:   ./addclient 127.0.0.1:7788 45 55

    4.10 工程包下载
         gSoapTest.zip   
阅读(6704) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~