首页 > 代码库 > gsoap入门实例
gsoap入门实例
- 环境
VS2008,gsoap_2.8,win7 - 实例场景:在客户端输入一个字符串,然后传递给服务端计算字符串长度并返回给客户端(附加一些加减乘除法的实现);
- 将..\gsoap-2.8\gsoap\bin\win32中的两个exe文件所在路径加入环境变量中,后面有用;
- 新建一个文件夹,设计一个calculator.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 int ns__add(int num1, int num2, int* result ); 8 int ns__sub(int num1, int num2, int* result ); 9 int ns__mult( int num1, int num2, int *result);10 int ns__divid( int num1, int num2, int *result);11 int ns__Hello(char* str,int *len);
- 在该文件夹中打开cmd(方法ctrl+右键 --》在此处打开命令窗口),输入命令:soapcpp2.exe calculator.h
有关gsoap的命令用法自行百度; - 你会发现生成很多很多文件
- 利用VS2008创建一个calServer的工程,将calculator.h add.nsmap soapC.cpp soapServer.cpp soapStub.h放入该过程,并且将gsoap-2.8\gsoap下的stdsoap2.h stdsoap2.cpp也添加到该过程;添加完后,在项目属性中的链接器--输入--附加依赖项 中输入wsock32.lib;
- 在calServer中添加一个main.cpp,代码如下:
1 #include "soapH.h" 2 #include "add.nsmap" 3 #include "stdio.h" 4 #include <iostream> 5 using namespace std; 6 7 int main( int argc, char *argv[]) 8 { 9 struct soap *CalculateSoap = soap_new(); //创建一个soap 10 int iSocket_master = soap_bind(CalculateSoap, NULL, 10000, 100); //绑定到相应的IP地址和端口()NULL指本机, 11 //10000为端口号,最后一个参数不重要。 12 if (iSocket_master< 0) //绑定出错 13 { 14 soap_print_fault(CalculateSoap, stderr); 15 exit(-1); 16 } 17 printf("SoapBind success,the master socket number is:%d\n",iSocket_master); //绑定成功返回监听套接字 18 19 while(1) 20 { 21 int iSocket_slaver = soap_accept(CalculateSoap); 22 if (iSocket_slaver < 0) 23 { 24 soap_print_fault(CalculateSoap, stderr); 25 exit(-2); 26 } 27 printf("Get a new connection,the slaver socket number is:%d\n",iSocket_slaver); //绑定成功返回监听套接字 28 soap_serve(CalculateSoap); 29 soap_end(CalculateSoap); 30 } 31 soap_done(CalculateSoap); 32 free(CalculateSoap); 33 34 return 0; 35 } 36 37 /*加法的具体实现*/ 38 int ns__add(struct soap *soap, int num1, int num2, int* result ) 39 { 40 if (NULL == result) 41 { 42 printf("Error:The third argument should not be NULL!\n"); 43 return SOAP_ERR; 44 } 45 else 46 { 47 (*result) = num1 + num2; 48 return SOAP_OK; 49 } 50 return SOAP_OK; 51 } 52 53 /*减法的具体实现*/ 54 int ns__sub(struct soap *soap,int num1, int num2, int* result ) 55 { 56 if (NULL == result) 57 { 58 printf("Error:The third argument should not be NULL!\n"); 59 return SOAP_ERR; 60 } 61 else 62 { 63 (*result) = num1 - num2; 64 return SOAP_OK; 65 } 66 return SOAP_OK; 67 } 68 69 /*乘法的具体实现*/ 70 int ns__mult(struct soap *soap, int num1, int num2, int *result) 71 { 72 if (NULL == result) 73 { 74 printf("Error:The third argument should not be NULL!\n"); 75 return SOAP_ERR; 76 } 77 else 78 { 79 (*result) = num1 * num2; 80 return SOAP_OK; 81 } 82 return SOAP_OK; 83 } 84 85 /*除法的具体实现*/ 86 int ns__divid(struct soap *soap, int num1, int num2, int *result) 87 { 88 if (NULL == result || 0 == num2) 89 { 90 printf("Error:The second argument is 0 or The third argument is NULL!\n"); 91 return SOAP_ERR; 92 } 93 else 94 { 95 (*result) = num1 / num2; 96 return SOAP_OK; 97 } 98 return SOAP_OK; 99 }100 101 int ns__Hello(struct soap *soap, char *str, int *len)102 {103 /*if (NULL == result)104 {105 printf("Error:The third argument should not be NULL!\n");106 return SOAP_ERR;107 }108 else109 {110 cout << result <<endl;111 return SOAP_OK;112 }*/113 //if (NULL == result)114 cout << str <<endl;115 (*len) = strlen(str);116 return SOAP_OK;117 118 }
- Server端完成,可以运行了。
- 客户端:利用VS2008创建一个calClient的工程,将calculator.h add.nsmap soapC.cpp soapClient.cpp soapStub.h放入该过程,并且将gsoap-2.8\gsoap下的stdsoap2.h stdsoap2.cpp也添加到该过程;添加完后,在项目属性中的链接器--输入--附加依赖项 中输入wsock32.lib;
- 在calClient中添加一个main.cpp,代码如下:
1 #include "soapH.h" 2 #include "add.nsmap" 3 #include "stdio.h" 4 #include <iostream> 5 using namespace std; 6 7 int main( int argc, char *argv[]) 8 { 9 printf("The Client is runing...\n");10 int num1 = 110;11 int num2 = 11;12 int result = 0;13 14 struct soap *CalculateSoap = soap_new();15 //soap_init(CalculateSoap);16 注意改你的ip,端口10000不改17 char * server_addr = "http://xx.x.x.x:10000";18 19 int iRet = soap_call_ns__add(CalculateSoap,server_addr,"",num1,num2,&result);20 if ( iRet == SOAP_ERR)21 {22 printf("Error while calling the soap_call_ns__add");23 }24 else25 {26 printf("Calling the soap_call_ns__add success。\n");27 printf("%d + %d = %d\n",num1,num2,result);28 }29 30 iRet = soap_call_ns__sub(CalculateSoap,server_addr,"",num1,num2,&result);31 if ( iRet == SOAP_ERR)32 {33 printf("Error while calling the soap_call_ns__sub");34 }35 else36 {37 printf("Calling the soap_call_ns__sub success。\n");38 printf("%d - %d = %d\n",num1,num2,result);39 }40 41 iRet = soap_call_ns__mult(CalculateSoap,server_addr,"",num1,num2,&result);42 if ( iRet == SOAP_ERR)43 {44 printf("Error while calling the soap_call_ns__mult");45 }46 else47 {48 printf("Calling the soap_call_ns__mult success。\n");49 printf("%d * %d = %d\n",num1,num2,result);50 }51 52 iRet = soap_call_ns__divid(CalculateSoap,server_addr,"",num1,num2,&result);53 if ( iRet == SOAP_ERR)54 {55 printf("Error while calling the soap_call_ns__divid");56 }57 else58 {59 printf("Calling the soap_call_ns__divid success。\n");60 printf("%d / %d = %d\n",num1,num2,result);61 }62 char *str = new char[1024];63 cin.getline(str,1024);64 cout << str <<endl;65 int len;66 iRet = soap_call_ns__Hello(CalculateSoap,server_addr,"",str,&len);67 if ( iRet == SOAP_ERR)68 {69 printf("Error while calling the soap_call_ns__add");70 }71 else72 {73 cout << str << " length is " << len <<" success!\n";74 }75 76 soap_end(CalculateSoap);77 soap_done(CalculateSoap);78 free(CalculateSoap);79 80 return 0;81 }
- Client端完成,可以先运行Server,在运行Client看看效果。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。