gSOAP 是 C/C++ 實現 SOAP Web Service 的成熟工具包,支援 WSDL 自動生成骨架代碼、XML 序列化、HTTP/HTTPS 傳輸。本文示範基本編譯流程與客戶端/伺服端架構。
# 下載
wget https://ncu.dl.sourceforge.net/project/gsoap2/gSOAP/gsoap_2.8.22.zip
# 或 RPM
rpm -ivh gsoap-2.7.8c-1mdv2007.0.x86_64.rpm
.h 定義服務介面(或從 WSDL 生成)。soapcpp2 生成骨架代碼(soapServer.cpp, soapClient.cpp, soapH.h, soapStub.h 等)。libgsoap。// service.h
int add(int a, int b, int *result); // SOAP RPC 風格
// server.c
#include "soapH.h"
#include "soapStub.h"
int add(struct soap *soap, int a, int b, int *result) {
*result = a + b;
return SOAP_OK;
}
int main() {
struct soap *soap = soap_new();
soap_bind(soap, NULL, 8080, 100);
while (1) {
struct soap *conn = soap_accept(soap);
if (!conn) break;
soap_serve(conn);
soap_destroy(conn);
soap_end(conn);
soap_free(conn);
}
soap_done(soap);
return 0;
}
// client.c
#include "soapH.h"
#include "soapStub.h"
int main() {
struct soap *soap = soap_new();
int result;
if (soap_call_add(soap, "https://localhost:8080", "", 3, 5, &result) == SOAP_OK) {
printf("3 + 5 = %d\n", result);
} else {
soap_print_fault(soap, stderr);
}
soap_destroy(soap);
soap_end(soap);
soap_free(soap);
return 0;
}
# 生成骨架
soapcpp2 -I/usr/local/share/gsoap/import service.h
# 編譯伺服端
gcc -o server server.c soapC.c soapServer.c stdsoap2.c -lpthread
# 編譯客戶端
gcc -o client client.c soapC.c soapClient.c stdsoap2.c
soapcpp2 -w service.h 產生 WSDL 供跨語言調用。soap_accept + poll/epoll 實現高併發。soap->ssl_flags 設定。xop:Include / MTOM 傳輸大型二進制資料。