下載地址:http://sourceforge.net/projects/gsoap2/files/c++
解壓安裝:./configure --prefix=/usr/local/gsoap && make && make installweb
示例目錄:gsoap-2.8/gsoap/samples 有各種語言使用接口方法 json
async chaining gmt Makefile mashup README.txt udp xml-rpc-json atom chaining++ googleapi Makefile.am mashup++ rest varparam autotest curl hello Makefile.cpp_proxy_rules mtom roll wcf aws databinding httpcookies Makefile.cpp_proxy_rules_j mtom-stream router webserver calc dime link Makefile.cpp_rules oneway rss wsa calc++ dom link++ Makefile.c_rules oneway++ ssl wsrm calc_vs2005 factory lu Makefile.defines polytest template wsse calc_xcode factorytest magic Makefile.in primes testmsgr wst
定義函數接口,寫chw.h文件api
//gsoap ns service name: chw Simple calculator service //gsoap ns service style: rpc //gsoap ns service encoding: encoded //gsoap ns service namespace: //gsoap ns service location: //gsoap ns schema namespace: urn:chw //gsoap ns service method-documentation: add Sums two values int add(int a, int b, int *result); //gsoap ns service method-documentation: sub Subtracts two values int sub(int a, int b, int *result);
使用命令:soapcpp2 -c -x chw.hxcode
-c 產生純C代碼,不然是C++代碼(與頭文件有關)bash
-I 指定import路徑cookie
-x 不要產生XML示例文件dom
生成文件有:curl
[root@Logcen5 test]# ls chw.h soapClient.c soapH.h soapServer.c soapStub.h soapC.c soapClientLib.c soap.nsmap soapServerLib.c
查看soapStub.h文件socket
/******************************************************************************\ * * * Client-Side Call Stub Functions * * * \******************************************************************************/ /** Web service synchronous operation 'soap_call_add' to the specified endpoint and SOAP Action header, returns SOAP_OK or error code */ SOAP_FMAC5 int SOAP_FMAC6 soap_call_add(struct soap *soap, const char *soap_endpoint, const char *soap_action, int a, int b, int *result); /** Web service asynchronous operation 'soap_send_add' to send a request message to the specified endpoint and SOAP Action header, returns SOAP_OK or error code */ SOAP_FMAC5 int SOAP_FMAC6 soap_send_add(struct soap *soap, const char *soap_endpoint, const char *soap_action, int a, int b); /** Web service asynchronous operation 'soap_recv_add' to receive a response message from the connected endpoint, returns SOAP_OK or error code */ SOAP_FMAC5 int SOAP_FMAC6 soap_recv_add(struct soap *soap, int *result); /** Web service synchronous operation 'soap_call_sub' to the specified endpoint and SOAP Action header, returns SOAP_OK or error code */ SOAP_FMAC5 int SOAP_FMAC6 soap_call_sub(struct soap *soap, const char *soap_endpoint, const char *soap_action, int a, int b, int *result); /** Web service asynchronous operation 'soap_send_sub' to send a request message to the specified endpoint and SOAP Action header, returns SOAP_OK or error code */ SOAP_FMAC5 int SOAP_FMAC6 soap_send_sub(struct soap *soap, const char *soap_endpoint, const char *soap_action, int a, int b); /** Web service asynchronous operation 'soap_recv_sub' to receive a response message from the connected endpoint, returns SOAP_OK or error code */ SOAP_FMAC5 int SOAP_FMAC6 soap_recv_sub(struct soap *soap, int *result); /******************************************************************************\ * * * Server-Side Operations * * * \******************************************************************************/ /** Web service operation 'add' implementation, should return SOAP_OK or error code */ SOAP_FMAC5 int SOAP_FMAC6 add(struct soap*, int a, int b, int *result); /** Web service operation 'sub' implementation, should return SOAP_OK or error code */ SOAP_FMAC5 int SOAP_FMAC6 sub(struct soap*, int a, int b, int *result);
另外須要從gsoap拷貝兩個文件過來stdsoap2.c和stdsoap2.h,方便gcc編譯不報錯!
編寫本身的服務端。server.c
#include <stdio.h> #include "soapH.h" #include "soap.nsmap" int main(int argc, char *argv[]) { struct soap soap; SOAP_SOCKET m, s; int ret; soap_init(&soap); if (argc < 2) soap_serve(&soap); else { m = soap_bind(&soap, NULL, atoi(argv[1]), 100); if (!soap_valid_socket(m)) { soap_print_fault(&soap, stderr); exit(-1); } fprintf(stderr, "Socket connection successful: master socket = %d\n", m); for ( ; ; ) { s = soap_accept(&soap); fprintf(stderr, "Socket connection successful: slave socket = %d\n", s); if (!soap_valid_socket(s)) { soap_print_fault(&soap, stderr); exit(-1); } soap_serve(&soap); soap_end(&soap); } } return 0; } int add(struct soap *soap, int a, int b, int *result) { *result = a + b; return SOAP_OK; } int sub(struct soap *soap, int a, int b, int *result) { *result = a - b; return SOAP_OK;
}
編譯命令:gcc -o ser server.c soapC.c soapServer.c stdsoap2.c 生成可執行程序 ser
編寫本身的客戶端。client.c
#include "soapH.h" #include "soap.nsmap" const char server[] = "http://localhost:8888"; int main(int argc, char *argv[]) { struct soap soap; int a, b, result; if (argc < 4) { fprintf(stderr, "Usage: [add|sub|mul|div|pow] num num\n"); exit(0); } a = atoi(argv[2]); b = atoi(argv[3]); soap_init(&soap); switch(*argv[1]) { case 'a': //soap_call_ns__add(&soap, server, "", a, b, &result); soap_call_add(&soap, server, "", a, b, &result); break; case 's': //soap_call_ns__sub(&soap, server, "", a, b, &result); soap_call_sub(&soap, server, "", a, b, &result); break; default: fprintf(stderr, "Unknown command\n"); exit(0); } if (soap.error) { soap_print_fault(&soap, stderr); exit(1); } else printf("result = %d\n", result); soap_destroy(&soap); soap_end(&soap); soap_done(&soap); return 0; }
編譯命令:gcc -o cle client.c soapC.c soapClient.c stdsoap2.c生成可執行程序 cle
測試:
在一個窗口執行:./ser 8888
[root@Logcen5 test]# ./ser 8888 Socket connection successful: master socket = 3
另一個窗口執行:./cle add 2 3
[root@Logcen5 test]# ./cle add 2 3 result = 5