gsoap使用心得!

From: http://www.cppblog.com/qiujian5628/archive/2008/06/19/54019.html


完整源码下载

最近换了个工作环境,现在在大望路这边上班,呵,刚上班接到的任务就是熟悉gsoap!废话少说,现在开始gSoap学习!

gSOAP是一个夸平台的,用于开发Web Service服务端和客户端的工具,在Windows、Linux、MAC OS和UNIX下使用C和C++语言编码,集合了SSL功能。

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

官方网站:http://genivia.com/Products/gsoap/index.html

对于Windows平台下开发客户端,首先下载最新的gsoap_win32_2.7.6c.zip包,具体在以下地址:http://optusnet.dl.sourceforge.net/sourceforge/gsoap2/gsoap_win32_2.7.6c.zip

首先查看gsoap的User's Guide,基本就能对gsoap有个全面的了解,通过阅读Sample里的例子程序深入。然后搜索网上其它一些文章,比如:
gSOAP简单多线程服务器程序 http://blog.chinaunix.net/u1/55091/showart_430965.html
纯c gSoap实现WebService            http://hi.baidu.com/2sky2sea/blog/item/40ec5555680279c1b745ae9b.html 


接下来我结合自己的实践与理解,讲讲VC用gsoap下编写webService和客户端程序,有不对的地方还请大家指正,谢谢。
我以网上出现的实现一个简单的加法函数为例,讲讲我在操作过程中遇到的问题。

一 服务器端
1.首先编写 add.h文件:
1//gsoap ns service name: add
2//gsoap ns service namespace: http://localhost/add.wsdl
3//gsoap ns service location: http://localhost
4//gsoap ns service executable: add.cgi
5//gsoap ns service encoding: encoded
6//gsoap ns schema namespace: urn:add
7
8int ns__add( int num1, int num2, int* sum );
9

2.用gsoap/bin目录下的soapcpp2.exe程序,生成一些文件。可以把soapcpp2.exe拷贝到一add.h目录下,用cmd执行soapcpp2.exe add.h就可以,在这个目录下会自动生成许多将来有用的文件,如add.namap,soapH.h,soapC.cpp,soapClient.cpp,soapServer.cpp等文件。soapcpp2.exe可以带参数执行,具体执行soapcpp2.exe -h查看。

3.新建一个win32控制台工程,加入wsock32.lib库,将刚才生成的那些文件添加到工程中。然后编写webserver.cpp主程序:
#include "add.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;
}


4. 编译这个程序,会提示错误,将gsoap_win32目录下stdsoap2.cpp,stdsoap2.h文件加入工程,重新编译如果还有错误,可能是你将add.h生成的文件添加入工程出错的原因。实际上在编写server程序时,无须带Client的那些文件,还有带Lib的文件也无须添加到工程中。再重新编译应该就没有问题了,启动4567端口,在ie中输入localhost:4567,如果显示xml页面,说明程序已经启动。

二 对应的客户端
1。客户端程序代码如下:
#include <stdio.h>
#include 
<stdlib.h>
#include 
"soapH.h"
#include 
"add.nsmap"


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

int main(int argc, char **argv) 
{
    
int result = -1;
    
char* server="http://localhost:4567";
    
int num1 = 0;
    
int num2 = 0;
    
int sum = 0;
    
if( argc < 3 )
    
{
        printf(
"usage: %s num1 num2 \n", 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\n", result);
    }

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

    
return 0;
}


int add( const char* server, int num1, int num2, int *sum )
{
    
struct soap add_soap;
    
int result = 0;
    soap_init(
&add_soap);
//    soap_set_namespaces(&add_soap, add_namespaces);
    
    
//该函数是客户端调用的主要函数,后面几个参数和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\n", 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;
}

2.客户端程序既可以新建一个新的win32控制台程序,将刚才生成的nsmap,soapH.h,soapClient.h等文件加入工程,编译既可。我是直接在原先工程中加入一客户端代码,将webserver.cpp文件移除,并且将soapServer.cpp等server端需要的文件移除,将soapClient.cpp等client端需要的cpp添加到工程,编译既可。
3.启动server程序,F5客户端程序,经测试正常。

三 遇到的问题
1.server端可以编译成CGI方式执行,而并不是绑定到某个端口,这种方式我没有实践。
if (argc < 2// no args: assume this is a CGI application 
   
      soap_serve(
&soap); // serve request, one thread, CGI style 
      soap_destroy(&soap); // dealloc C++ data 
      soap_end(&soap); // dealloc data and clean up 
}
2.在编译服务器及客户端程序时一开始对add.h生成的文件添加到工程,经常出现问题,需要自己不调试。特别是链接时段,server/client要与其生成的文件相对应,server调用生成的soapserver.cpp,client调用生成的soapclient.cpp文件。
3.多线程方式,在windows下建议用pthread_win32库,这里给出多线程下的例子。
一 gSOAP需要的头文件:

// gsoap ns service name: calc
// gsoap ns service style: rpc
// gsoap ns service encoding: encoded
// gsoap ns service namespace:  http://127.0.0.1 :8089/calc.wsdl
// gsoap ns service location:  http://127.0.0.1 :8089/cal
// gsoap ns schema  namespace:    urn:calc
int  ns__add( double  a,  double  b,  double   * result);
int  ns__sub( double  a,  double  b,  double   * result);
int  ns__mul( double  a,  double  b,  double   * result);
int  ns__div( double  a,  double  b,  double   * result);
int  ns__pow( double  a,  double  b,  double   * result);

二 多线程服务器关键代码

#include 
#include  
" calc.nsmap "
#include  
" soapH.h "

/
///宏与全局变量的定义

#define   BACKLOG (100)  
#define   MAX_THR (10)   
#define   MAX_QUEUE (1000)


pthread_mutex_t queue_cs;                        
// 队列锁
pthread_cond_t  queue_cv;                           // 条件变量
SOAP_SOCKET     queue[MAX_QUEUE];    // 数组队列
int                            head  = 0 , tail  = 0 ;           // 队列头队列尾初始化         
//


//

void   *       process_queue( void   * );         // 线程入口函数
int          enqueue(SOAP_SOCKET);   // 入队列函数
SOAP_SOCKET dequeue( void );          // 出队列函数

//
// 线程入口函数
void   *  process_queue( void   *  soap)
{
  
struct soap * tsoap = (struct soap *)soap;
  
for(;;)
  
{
        tsoap
->socket = dequeue();
        
if (!soap_valid_socket(tsoap->socket))
       
{
         
break;
        }

        soap_serve(tsoap);
        soap_destroy(tsoap);
        soap_end(tsoap);
  }

  
return NULL;
}


// 入队列操作
int  enqueue(SOAP_SOCKET sock)
{
  
int status = SOAP_OK;
  
int next;
  pthread_mutex_lock(
&queue_cs);
  next 
= tail +1;
  
if (next >= MAX_QUEUE) 
    next 
= 0;
  
if (next == head) 
      status 
= SOAP_EOM;
  
else
  
{
    queue[tail] 
=sock;
    tail 
= next;
  }

  pthread_cond_signal(
&queue_cv);
  pthread_mutex_unlock(
&queue_cs);
  
return status;
}


// 出队列操作
SOAP_SOCKET dequeue()
{
  SOAP_SOCKET sock;
  pthread_mutex_lock(
&queue_cs);
   
while (head == tail )
   
{
          pthread_cond_wait(
&queue_cv,&queue_cs);
   }

  sock 
= queue[head++];
  
if (head >= MAX_QUEUE)
        
{
    head 
=0;
  }

  pthread_mutex_unlock(
&queue_cs);
  
return sock;
}



//具体服务方法////
// 加法的实现
int  ns__add( struct  soap  * soap,  double  a,  double  b,  double   * result)
{
      
*result = a + b;
      
return SOAP_OK;
}
 
// 减法的实现
int  ns__sub( struct  soap  * soap,  double  a,  double  b,  double   * result)

     
*result = a - b;
     
return SOAP_OK;
}
 
// 乘法的实现
int  ns__mul( struct  soap  * soap,  double  a,  double  b,  double   * result)

     
*result = a * b;
     
return SOAP_OK;
}
 
// 除法的实现
int  ns__div( struct  soap  * soap,  double  a,  double  b,  double   * result)

   
if (b)
       
*result = a / b;
   
else
  
{
         
char *= (char*)soap_malloc(soap, 1024);
         sprintf(s, 
"Can't">http://tempuri.org/">Can't divide %f by %f", a, b);
         return soap_sender_fault(soap, "Division by zero", s);
  }

  
return SOAP_OK;
}
 
// 乘方的实现
int  ns__pow( struct  soap  * soap,  double  a,  double  b,  double   * result)

  
*result = pow(a, b);
  
if (soap_errno == EDOM) /* soap_errno 和errorno类似, 但是和widnows兼容 */
  

    
char *= (char*)soap_malloc(soap, 1024);
    sprintf(s, 
"Can't take the power of %f to  %f", a, b);
    sprintf(s, 
"Can't">http://tempuri.org/">Can't take power of %f to %f", a, b);
    return soap_sender_fault(soap, "Power function domain error", s);
  }

  
return SOAP_OK;
}
 

//
// 主函数
int  main( int  argc, char   **  argv)
{
  
struct soap ServerSoap;
     
//初始话运行时环境
    soap_init(&ServerSoap);
    
//如果没有参数,当作CGI程序处理
    if (argc <2
    
{       
           
//CGI 风格服务请求,单线程
          soap_serve(&ServerSoap);
          
//清除序列化的类的实例
         soap_destroy(&ServerSoap);
         
//清除序列化的数据
        soap_end(&ServerSoap);     
   }
else
   
{
     
struct soap * soap_thr[MAX_THR];
     pthread_t tid[MAX_THR];
     
int i,port = atoi(argv[1]);
     SOAP_SOCKET m,s;
      
//锁和条件变量初始化
     pthread_mutex_init(&queue_cs,NULL);
     pthread_cond_init(
&queue_cv,NULL);
     
//绑定服务端口
    m = soap_bind(&ServerSoap,NULL,port,BACKLOG);
    
//循环直至服务套接字合法
    while (!soap_valid_socket(m))
   
{
                fprintf(stderr,
"Bind port error! ");
                m 
= soap_bind(&ServerSoap,NULL,port,BACKLOG);
    }

    fprintf(stderr,
"socket connection successful %d ",m);
                
     
//生成服务线程
    for(i = 0; i <MAX_THR; i++)

   
{
      soap_thr[i] 
= soap_copy(&ServerSoap);
      fprintf(stderr,
"Starting thread %d ",i);
      pthread_create(
&tid[i],NULL,(void*(*)(void*))process_queue,(void*)soap_thr[i]);
    }

                
    
for(;;)
    
{
      
//接受客户端的连接
      s = soap_accept(&ServerSoap);
      
if (!soap_valid_socket(s)) 
      
{
        
if (ServerSoap.errnum) 
                                
{
          soap_print_fault(
&ServerSoap,stderr);
          
continue;
        }
else
        
{
          fprintf(stderr,
"Server timed out ");
          
break;
        }

      }

       
//客户端的IP地址
      fprintf(stderr,"Accepted connection from IP= %d.%d.%d.%d socket = %d ",
                               ((ServerSoap.ip)
>>24)&0xFF,((ServerSoap.ip)>>16)&0xFF,((ServerSoap.ip)>>8)&0xFF,(ServerSoap.ip)&0xFF,(ServerSoap.socket));
      
//请求的套接字进入队列,如果队列已满则循环等待
       while(enqueue(s) == SOAP_EOM)
                Sleep(
1000);
    }

    
//服务结束后的清理工作
    for(i = 0; i < MAX_THR; i++)
    
{
      
while (enqueue(SOAP_INVALID_SOCKET) == SOAP_EOM) 
       
{
           Sleep(
1000);
      }

    }

    
for(i=0; i< MAX_THR; i++)
    
{
      fprintf(stderr,
"Waiting for thread %d to terminate ..",i);
      pthread_join(tid[i],NULL);
      fprintf(stderr,
"terminated ");
      soap_done(soap_thr[i]);
      free(soap_thr[i]);
    }

    pthread_mutex_destroy(
&queue_cs);
    pthread_cond_destroy(
&queue_cv);
  }

    
//分离运行时的环境
  soap_done(&ServerSoap);
  
return 0;
}


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/405598.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

android tabhost 多个activity,Android:TabHost中Activity的生命周期问题

用过TabHost制作多个activity的分页效果的朋友应该知道&#xff0c;tabhost中镶嵌的activity的onCreate和onDestroy是和tabhost关联的&#xff0c;在创建了tabhost之后&#xff0c;第一次访问某个activity会执行这个activity的oncreate事件&#xff0c;以后切回来时只会执行onR…

网站决策分析软件WebBI

为了提高网站的访问量、黏合度&#xff0c;网站的管理者投入大量的精力于市场推广、内容发布、网站业务改进等&#xff0c;而对于如何评估推广效果、如何了解网民的行为以提高网站的吸引力和服务质量很多网站都显的无力可施。经过互联网多年的发展&#xff0c;互联网网站之间的…

eclipse启动报错No java virtual machine was found after seearching the locations:XXXXX

一、问题描述 卸载之前的老版本JDK后&#xff0c;重新安装了JDK&#xff0c;再次打开eclipse出现以下问题&#xff1a;在指定的目录中找不到JDK虚拟机。 二、解决办法 eclipse启动时会根据eclipse.ini配置文件来找javaw.exe 启动ecplise GUI。 在eclipse安装的根目录中找到e…

网卡配置

网卡配置文件 &#xff08;1&#xff09;/etc/sysconfig/network-scripts/ifcfg-interface-name 配置文件ifcfg-interface-name包含了初始化接口所需的大部分详细信息。其中interface-name将根据网卡的类型和排序而不同&#xff0c;一般其名字为eth0、eth1、ppp0等&#xff0c…

linux ace platform_linux.gnu,Linux下配置和使用ACE笔记

1. 下载 ACE 5.7.从 http://download.dre.vanderbilt.edu/ 下载 ACETAOCIAO-5.7.tar.gz包。2. 解压将压缩包copy到linux目录/data/ACE里&#xff0c;然后解压, tar -zxvf ACETAOCIAO-5.7.tar.gz。解压后的目录是 /data/ACE_wrappers.3. 配置config.h和platform_macros.GNU。…

Linux 查看磁盘分区、文件系统、使用情况的命令和相关工具介绍

From: http://fedora.linuxsir.org/main/?qnode/41 作者&#xff1a;北南南北 来自&#xff1a;LinuxSir.Org 提要&#xff1a;Linux 磁盘分区表、文件系统的查看、统计的工具很多&#xff0c;有些工具是多功能的&#xff0c;不仅仅是查看磁盘的分区表&#xff0c;而且也能进行…

Android 机顶盒手势、数据分页演示DEMO

上篇讲了一篇机顶盒的一个封装类《GridView》&#xff0c;利用这个封装的GridView 可以实现一些例如移动、位移图标等基础组件“GridView”无法实现的一些功能。本篇将会提出一部分可供运行的代码演示一个分页、手势换屏的DEMO版本&#xff0c;供大家学习。 DEMO 的工程目录如下…

Java jdk中的部分工具javac javaw javaws javadoc javah javap jar jdb jps的作用

一、JDK 工具和实用程序 JDK提供的标准工具和程序 基本工具&#xff1a;javac, java, javadoc, apt, appletviewer, jar, jdb, javah, javap, extcheck安全工具&#xff1a;keytool, jarsigner, policytool, kinit, klist, ktab国际化工具&#xff1a;native2ascii远程方法调…

firefox android 去更新,Android版Firefox Beta发布更新

就在谷歌I/O大会前夕&#xff0c;Android版Firefox Beta悄悄的发布了一次更新。更新内容包括&#xff1a;Flash支持(虽然事实上在上个月的更新中就已经支持了)以及日常bug修复及稳定性提升等&#xff0c;不过依然没有加入复制和粘贴功能。此外Firefox Beta的UI界面也被重新设计…

Linux/UNIX的scp命令用法详解

From: http://www.ha97.com/4169.html PS&#xff1a;偶很喜欢用scp这个命令来传输数据。scp命令跟cp命令类似&#xff0c;只不过cp命令是在同一台机器上用的&#xff0c;scp是在两台机器上复制传输数据的命令。scp实质相当于利用SSH协议来传输数据的cp命令。 用法举例&#xf…

React开发(230):ant design table固定表头

固定表头 方便一页内展示大量数据。需要指定 column 的 width 属性&#xff0c;否则列头和内容可能不对齐。 如果指定 width 不生效或出现白色垂直空隙&#xff0c; 请尝试建议留一列不设宽度以适应弹性布局&#xff0c;或者检查是否有超长连续字段破坏布局。

安装ARM交叉编译工具

1. 下载交叉编译工具链。 可到如下两个网站下载&#xff1a; http://ftp.arm.linux.org.uk/pub/armlinux/toolchain/ http://www.uushare.com/user/zyh006814/file/1434053 2. 安装 # mkdir /usr/local/arm # cd /usr/local/arm # 解压下载的工具链 # 添加环境变量&#xff0c…

Linux下获得线程ID syscall(224)

From: http://blog.163.com/onx0320126/blog/static/164736436201252574720314/ 对于Linux线程&#xff0c;也许大家并不陌生&#xff0c;而我们通常所说的线程是指基于POSIX标准的线程&#xff0c;而Linux下除了兼容POSIX标准&#xff0c;而且提供线程在内核级的支持。早在Lin…

Qt中常用的QChar QByteArry QString数据类型转换方法

一、QChar类型 转 QByteArry类型 char mybuffer[mybufferSize]; QByteArray ByteData QByteArray(mybuffer, mybufferSize);二、QByteArry类型 转 QString类型 QByteArray _byte; QString string&#xff0c;_string1; string QString(_byte); _string1 QString(_byte.toH…

android studio 安装问题,解决Android Studio的安装问题

今天开始了android studio的下载与安装&#xff0c;我再官网上下载了Android studio,下载不难&#xff0c;运行出来可需要一定的时间&#xff0c;在中途中我遇到了一些问题一&#xff1a;Build错误&#xff1a;在我最开始下载完Android studio时&#xff0c;我新建项目的时候&a…

React开发(231):常见手机号邮箱的正则

// 手机号 const isPhone /^1[3-9]\d{9}$/; // const isPhone /(\d{3}-|\d{4}-)?(\d{8}|\d{7})?/;// 邮箱 const isEmail /^([A-Za-z0-9_\-\.])\([A-Za-z0-9_\-\.])\.([A-Za-z]{2,4})$/; // 密码格式 const isPassword /^[\w~!#$%^&*()-|\\<>,.?/\[\]{}:;&qu…

用JS访问iframe中的元素

用JS访问iframe中的元素 &#xff11;.首先&#xff0c;如何访问iframe中的元素&#xff1f;见如下代码&#xff1a;(1)1.htm源码&#xff1a;<input typebutton value"test" οnclick"self.frames[a].document.all.mybutton.disabledtrue"> <…

诺基亚7原生android,【诺基亚7Plus评测】系统:简洁原生安卓功能却不简单_诺基亚 7 Plus(4GB RAM/全网通)_手机评测-中关村在线...

系统&#xff1a;简洁原生安卓功能却不简单诺基亚 7 Plus的系统为Android 8.0&#xff0c;与国内厂商大刀阔斧的定制化相反&#xff0c;诺基亚 7 Plus的系统几近原生&#xff0c;仅仅内置了微信、微博、支付宝三款国民应用&#xff0c;而且这三个软件都可以卸载&#xff0c;最大…