QQ登录

只需一步,快速开始

gsoap 2 8 基于客户服务端的实例源码教程及使用方法

[ 复制链接 ]
附件是基于gsoap-2.8的如何编写客户端与服务器端教程,包含有gsoap-2.8的官网下载的源代码;
生成客户端与服务器端供我们vc或QT等语言使用的批处理工具;
以及这个工具的使用。还有几篇网络上的关于gsoap的教程
捕获.JPG


下面是附件的使用方法,一步步操作,生成客户端与服务器端程序:
依照下面的方法生成了附件中的例程;

0.解压附件,soapInterface.bat所在路径不得含中文 空格,例如:C:\Users\gkbc8\Desktop\newfolder

1.新建头文件 取soapInterface.bat文件的同名:soapInterface.h
文件内编写接口,具体说明看附件参考的教程

//gsoap ns service name: gservice
//gsoap ns service style: rpc

int ns__add(int num1, int num2, int* result );
int ns__sub(int num1, int num2, int* result );
int ns__mult( int num1, int num2, int *result);
int ns__divid( int num1, int num2, int *result);

2.从附件内gsoap-2.8包中搜索复制stdsoap2.h,stdsoap2.cpp,soapcpp2.exe,
存放于soapInterface.bat同级目录。附件内已操作好;

3.双击soapInterface.bat运行。生成gClientSoap,gServerSoap两个文件夹,分别复制到随后自己创建的服务器工程与客户端工程中使用

4.gClientSoap,gServerSoap两个文件夹内用到的文件功能说明,更多参考附件内的教程

1)soapC.cpp , soapH.h//soap的序列和反序列代码,它已经包含了soapStub.h

2)soapServer.c ppsoapServerLib.cpp //服务器端代码(纯C代码是soapServer.c soapServerLib.c ),soapServerLib.cpp文件则只是简单地包含soapServer.cpp和soapC.cpp

3)soapClient.cpp soapClientLib.cpp//客户端代码(纯C代码是soapClient.csoapClientLib.c ),soapClientLib.cpp文件则只是简单地包含soapClient.cpp和soapC.cpp

4) soapStub.h    // soap的存根文件,定义了我们编写的头文件里对应的远程调用模型

5) add.nsmap //XML服务命名空间

6)服务器端要载入的文件有:soapServer.cpp,soapC.cpp,stdsoap2.cpp;
           要包含的文件有:gservice.nsmap,soapH.h;
   客户端要输入的文件有:  soapClient.cpp,soapC.cpp,stdsoap2.cpp;
           要包含的文件有:gservice.nsmap,soapH.h



下面是例程服务器与客服程序中对gsaop的引用
  1. #include "stdafx.h"
  2. #include "stdio.h"

  3. #include "gSoapClient/soapH.h"
  4. #include "gSoapClient/gservice.nsmap"

  5. int main( int argc, char *argv[])
  6. {
  7.         int num1 = 110;
  8.         int num2 = 11;
  9.         int result = 0;

  10.         struct soap *CalculateSoap = soap_new();
  11.         //soap_init(CalculateSoap);
  12.         char * server_addr = "http://localhost:8080";

  13.         int iRet = soap_call_ns__add(CalculateSoap,server_addr,"",num1,num2,&result);
  14.         if ( iRet == SOAP_ERR)
  15.                 printf("Error while calling the soap_call_ns__add");
  16.         else
  17.         {
  18.                 printf("Calling the soap_call_ns__add success。\n");
  19.                 printf("%d + %d = %d\n",num1,num2,result);
  20.         }


  21.         iRet = soap_call_ns__sub(CalculateSoap,server_addr,"",num1,num2,&result);
  22.         if ( iRet == SOAP_ERR)
  23.                 printf("Error while calling the soap_call_ns__sub");
  24.         else
  25.         {
  26.                 printf("Calling the soap_call_ns__sub success。\n");
  27.                 printf("%d - %d = %d\n",num1,num2,result);
  28.         }

  29.         iRet = soap_call_ns__mult(CalculateSoap,server_addr,"",num1,num2,&result);
  30.         if ( iRet == SOAP_ERR)
  31.                 printf("Error while calling the soap_call_ns__mult");
  32.         else
  33.         {
  34.                 printf("Calling the soap_call_ns__mult success。\n");
  35.                 printf("%d * %d = %d\n",num1,num2,result);
  36.         }

  37.         iRet = soap_call_ns__divid(CalculateSoap,server_addr,"",num1,num2,&result);
  38.         if ( iRet == SOAP_ERR)
  39.                 printf("Error while calling the soap_call_ns__divid");
  40.         else
  41.         {
  42.                 printf("Calling the soap_call_ns__divid success。\n");
  43.                 printf("%d / %d = %d\n",num1,num2,result);
  44.         }

  45.         soap_end(CalculateSoap);
  46.         soap_done(CalculateSoap);
  47.         free(CalculateSoap);
  48.         return 0;
  49. }
复制代码
服务器端:
  1. #include "stdafx.h"
  2. #include "stdio.h"

  3. #include "gSoapServer/soapH.h"
  4. #include "gSoapServer/gservice.nsmap"

  5. int main(int argc,char **argv)
  6. {
  7.         int         nPort = 8080;
  8.         struct soap fun_soap;
  9.         soap_init(&fun_soap);

  10.         int nMaster = (int)soap_bind(&fun_soap, NULL, nPort, 100);
  11.         if (nMaster < 0)
  12.         {
  13.                 soap_print_fault(&fun_soap, stderr);
  14.                 exit(-1);
  15.         }
  16.         fprintf(stderr, "Socket connection successful : master socket = %d\n", nMaster);

  17.         //<>
  18.         while (true)
  19.         {
  20.                 int nSlave = (int)soap_accept(&fun_soap);
  21.                 if (nSlave < 0)
  22.                 {
  23.                         soap_print_fault(&fun_soap, stderr);
  24.                         exit(-1);
  25.                 }
  26.                 fprintf(stderr, "Socket connection successful : slave socket = %d\n", nSlave);

  27.                 soap_serve(&fun_soap);
  28.                 soap_end(&fun_soap);
  29.         }
  30.         return 0;
  31. }

  32. /*具体函数实现*/
  33. int ns__add(struct soap *soap, int num1, int num2, int* result )   
  34. {
  35.         if (NULL == result)
  36.         {
  37.                 printf("Error:The third argument should not be NULL!\n");
  38.                 return SOAP_ERR;
  39.         }
  40.         else
  41.         {
  42.                 (*result) = num1 + num2;
  43.                 return SOAP_OK;
  44.         }
  45.         return SOAP_OK;
  46. }

  47. int ns__sub(struct soap *soap,int num1, int num2, int* result )
  48. {
  49.         if (NULL == result)
  50.         {
  51.                 printf("Error:The third argument should not be NULL!\n");
  52.                 return SOAP_ERR;
  53.         }
  54.         else
  55.         {
  56.                 (*result) = num1 - num2;
  57.                 return SOAP_OK;
  58.         }
  59.         return SOAP_OK;
  60. }
  61. int ns__mult(struct soap *soap, int num1, int num2, int *result)
  62. {
  63.         if (NULL == result)
  64.         {
  65.                 printf("Error:The third argument should not be NULL!\n");
  66.                 return SOAP_ERR;
  67.         }
  68.         else
  69.         {
  70.                 (*result) = num1 * num2;
  71.                 return SOAP_OK;
  72.         }
  73.         return SOAP_OK;
  74. }

  75. int ns__divid(struct soap *soap, int num1, int num2, int *result)
  76. {
  77.         if (NULL == result || 0 == num2)
  78.         {
  79.                 printf("Error:The second argument is 0 or The third argument is NULL!\n");
  80.                 return SOAP_ERR;
  81.         }
  82.         else
  83.         {
  84.                 (*result) = num1 / num2;
  85.                 return SOAP_OK;
  86.         }
  87.         return SOAP_OK;
  88. }
复制代码


附件下载地址:
https://pan.baidu.com/s/1SvoqB5AqKRqyvoVAD_N7IA

回复

使用道具 举报

大神点评(1)

点击查看
快速回复 返回列表 客服中心 搜索