附件是基于gsoap-2.8的如何编写客户端与服务器端教程,包含有gsoap-2.8的官网下载的源代码;
生成客户端与服务器端供我们vc或QT等语言使用的批处理工具;
以及这个工具的使用。还有几篇网络上的关于gsoap的教程
下面是附件的使用方法,一步步操作,生成客户端与服务器端程序:
依照下面的方法生成了附件中的例程;
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的引用
- #include "stdafx.h"
- #include "stdio.h"
- #include "gSoapClient/soapH.h"
- #include "gSoapClient/gservice.nsmap"
- int main( int argc, char *argv[])
- {
- int num1 = 110;
- int num2 = 11;
- int result = 0;
- struct soap *CalculateSoap = soap_new();
- //soap_init(CalculateSoap);
- char * server_addr = "http://localhost:8080";
- int iRet = soap_call_ns__add(CalculateSoap,server_addr,"",num1,num2,&result);
- if ( iRet == SOAP_ERR)
- printf("Error while calling the soap_call_ns__add");
- else
- {
- printf("Calling the soap_call_ns__add success。\n");
- printf("%d + %d = %d\n",num1,num2,result);
- }
- iRet = soap_call_ns__sub(CalculateSoap,server_addr,"",num1,num2,&result);
- if ( iRet == SOAP_ERR)
- printf("Error while calling the soap_call_ns__sub");
- else
- {
- printf("Calling the soap_call_ns__sub success。\n");
- printf("%d - %d = %d\n",num1,num2,result);
- }
- iRet = soap_call_ns__mult(CalculateSoap,server_addr,"",num1,num2,&result);
- if ( iRet == SOAP_ERR)
- printf("Error while calling the soap_call_ns__mult");
- else
- {
- printf("Calling the soap_call_ns__mult success。\n");
- printf("%d * %d = %d\n",num1,num2,result);
- }
- iRet = soap_call_ns__divid(CalculateSoap,server_addr,"",num1,num2,&result);
- if ( iRet == SOAP_ERR)
- printf("Error while calling the soap_call_ns__divid");
- else
- {
- printf("Calling the soap_call_ns__divid success。\n");
- printf("%d / %d = %d\n",num1,num2,result);
- }
- soap_end(CalculateSoap);
- soap_done(CalculateSoap);
- free(CalculateSoap);
- return 0;
- }
复制代码 服务器端:
- #include "stdafx.h"
- #include "stdio.h"
- #include "gSoapServer/soapH.h"
- #include "gSoapServer/gservice.nsmap"
- int main(int argc,char **argv)
- {
- int nPort = 8080;
- struct soap fun_soap;
- soap_init(&fun_soap);
- int nMaster = (int)soap_bind(&fun_soap, NULL, nPort, 100);
- if (nMaster < 0)
- {
- soap_print_fault(&fun_soap, stderr);
- exit(-1);
- }
- fprintf(stderr, "Socket connection successful : master socket = %d\n", nMaster);
- //<>
- while (true)
- {
- int nSlave = (int)soap_accept(&fun_soap);
- if (nSlave < 0)
- {
- soap_print_fault(&fun_soap, stderr);
- exit(-1);
- }
- fprintf(stderr, "Socket connection successful : slave socket = %d\n", nSlave);
- soap_serve(&fun_soap);
- soap_end(&fun_soap);
- }
- return 0;
- }
- /*具体函数实现*/
- int ns__add(struct soap *soap, int num1, int num2, int* result )
- {
- if (NULL == result)
- {
- printf("Error:The third argument should not be NULL!\n");
- return SOAP_ERR;
- }
- else
- {
- (*result) = num1 + num2;
- return SOAP_OK;
- }
- return SOAP_OK;
- }
- int ns__sub(struct soap *soap,int num1, int num2, int* result )
- {
- if (NULL == result)
- {
- printf("Error:The third argument should not be NULL!\n");
- return SOAP_ERR;
- }
- else
- {
- (*result) = num1 - num2;
- return SOAP_OK;
- }
- return SOAP_OK;
- }
- int ns__mult(struct soap *soap, int num1, int num2, int *result)
- {
- if (NULL == result)
- {
- printf("Error:The third argument should not be NULL!\n");
- return SOAP_ERR;
- }
- else
- {
- (*result) = num1 * num2;
- return SOAP_OK;
- }
- return SOAP_OK;
- }
- int ns__divid(struct soap *soap, int num1, int num2, int *result)
- {
- if (NULL == result || 0 == num2)
- {
- printf("Error:The second argument is 0 or The third argument is NULL!\n");
- return SOAP_ERR;
- }
- else
- {
- (*result) = num1 / num2;
- return SOAP_OK;
- }
- return SOAP_OK;
- }
复制代码
附件下载地址:
https://pan.baidu.com/s/1SvoqB5AqKRqyvoVAD_N7IA
|