- 環境
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 soapH.h 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 else 109 { 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 soapH.h 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 else 25 { 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 else 36 { 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 else 47 { 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 else 58 { 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 else 72 { 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看看效果。
注意:客戶端和服務端可以再兩台電腦上允許並訪問。