一、gSOAP簡介
gSOAP一種跨平臺的C和 C++軟件開發工具包。生成C/C++的RPC代碼,XML數據綁定,對SOAP Web服務和其他應用形成高效的具體架構解析器,它們都受益於一個XML接口。 這個工具包提供了一個全面和透明的XML數據綁定解決方案,Autocoding節省大量開發時間來執行SOAP/XML Web服務中的C/C++。此外,使用XML數據綁定大大簡化了XML自動映射。應用開發人員不再需要調整應用程序邏輯的具體庫和XML爲中心的數據,如 交涉DOM。
二、gSOAP安裝
1、準備工作
操作系統:centos6.5
位數:64位
gSOAP下載地址:http://sourceforge.net/projects/gsoap2/files/
本文gSOAP爲:gsoap_2.8.33.zip
2、解壓源碼
#unzip gsoap_2.8.33.zip
3、配置
#cd gsoap-2.8
#./configure --prefix=/usr/local/gSOAP
4、編譯安裝
#make
三、make出錯處理
(1)ylwrap: line 176: yacc: command not found。
yacc是一個生成語法分析器的工具。
#yum install byacc
(2)missing: line 81: flex: command not found。
#yum install flex
(需重新配置安裝路徑)
(3)/usr/bin/ld: cannot find -ly
#yum install yum install bison-devel
(4)../../gsoap/stdsoap2.h:690:19:error:zlib.h:No such file or directory
#yum install zlib-devel
(5)error: openssl/bio.h: No such file or directory
#yum install openssl-devel
(6)undefined reference to soap_ssl_init'
出現該錯誤,回到上級目錄,刪除我們解壓的目錄,重新解壓,執行第二步gSOAP安裝,make後未報錯誤,執行
#make install
安裝成功,會在/usr/local/gSOAP目錄下生成我們需要的文件
下載相應的包,主要有2個工具和源代碼:
wsdl2h -o outfile.h infile.wsdl 實現wsdl文件到h文件的數據映射
soapcpp2 -c outfile.h生成相應的底層通信stub,strech程序
四、簡單測試程序
1、頭文件
下面這個簡單的例子實現的是在客戶端輸入2個數字,然後遠程調用服務端的加法函數,乘法函數,最後返回結果給客戶端。
在這裏我們不需要wsdl的文件,可以直接從.h文件來生成代碼。我們新建一個文件夾soap,我們定義一個函數聲明文件,用來定義接口函數,名稱爲add.h,內容如下:
//gsoapopt cw
//gsoap ns2 schema namespace: urn:add
//gsoap ns2 schema form: unqualified
//gsoap ns2 service name: add
//gsoap ns2 service type: addPortType
add http://schemas.xmlsoap.org/soap/encoding/
//gsoap ns2 service method-action: add ""
int ns2__add( int num1, int num2, int* sum );
int ns2__sub( int num1, int num2, int *sub);
然後我們執行soapcpp2 -c add.h,自動生成一些遠程調用需要的文件。由於我們生成在/usr/local/gSOAP目錄下,因此
#/usr/local/gSOAP/bin/soapcpp2 -c add.h
2、服務端,創建文件addserver.c
#include <string.h>
#include <stdio.h>
#include "soapH.h"
#include "add.nsmap"
int main(int argc, char **argv)
{
int m, s;
struct soap add_soap;
soap_init(&add_soap);
soap_set_namespaces(&add_soap, namespaces);
if (argc < 2) {
printf("usage: %s <server_port> /n", argv[0]);
exit(1);
} else {
m = soap_bind(&add_soap, NULL, atoi(argv[1]), 100);
if (m < 0) {
soap_print_fault(&add_soap, stderr);
exit(-1);
}
fprintf(stderr, "Socket connection successful: master socket = %d\n", m);
for (;;) {
s = soap_accept(&add_soap);
if (s < 0) {
soap_print_fault(&add_soap, stderr);
exit(-1);
}
fprintf(stderr, "Socket connection successful: slave socket = %d\n", s);
soap_serve(&add_soap);
soap_end(&add_soap);
}
}
return 0;
}
int ns2__add(struct soap *add_soap, int num1, int num2, int *sum)
{
*sum = num1 + num2;
return 0;
}
int ns2__sub(struct soap *sub_soap, int num1, int num2, int *sub)
{
*sub = num1 - num2;
return 0;
}
3、客戶端,文件addclient.c
#include <string.h>
#include <stdio.h>
#include "soapStub.h"
#include "add.nsmap"
int add(const char *server, int num1, int num2, int *sum);
int sub(const char *server, int num1, int num2, int *sub);
int main(int argc, char **argv)
{
int result = -1;
char server[128] = {0};
int num1;
int num2;
int sum;
if (argc < 4) {
printf("usage: %s <ip:port> num1 num2 /n", argv[0]);
exit(1);
}
strcpy(server,argv[1]);
num1 = atoi(argv[2]);
num2 = atoi(argv[3]);
result = add(server, num1, num2, &sum);
if (result != 0) {
printf("soap error, errcode=%d\n", result);
} else {
printf("%d + %d = %d\n", num1, num2, sum);
}
result = sub(server, num1, num2, &sum);
if (result != 0) {
printf("soap error, errcode=%d\n", result);
} else {
printf("%d - %d = %d\n", num1, num2, sum);
}
return 0;
}
int add(const char *server, int num1, int num2, int *sum)
{
struct soap add_soap;
int result = 0;
soap_init(&add_soap);
soap_set_namespaces(&add_soap, namespaces);
soap_call_ns2__add(&add_soap, server, NULL, num1, num2, sum);
printf("server is %s, num1 is %d, num2 is %d\n", server, num1, num2);
if (add_soap.error) {
printf("soap error: %d, %s, %s\n", add_soap.error, *soap_faultcode(&add_soap), *soap_faultstring(&add_soap));
result = add_soap.error;
}
soap_end(&add_soap);
soap_done(&add_soap);
return result;
}
int sub(const char *server, int num1, int num2, int *sub)
{
struct soap add_soap;
int result = 0;
soap_init(&add_soap);
soap_set_namespaces(&add_soap, namespaces);
soap_call_ns2__sub(&add_soap, server, NULL, num1, num2, sub);
printf("server is %s, num1 is %d, num2 is %d\n", server, num1, num2);
if (add_soap.error) {
printf("soap error: %d, %s, %s\n", add_soap.error, *soap_faultcode(&add_soap), *soap_faultstring(&add_soap));
result = add_soap.error;
}
soap_end(&add_soap);
soap_done(&add_soap);
return result;
}
到此爲止,我們自己的代碼已經編寫完畢,現在我們來編譯服務端和客戶端
注意:編譯的時候我們需要gsoap包裏的源代碼文件,把stdsoap2.c和stdsoap2.h文件拷貝到當前目錄
#cp /opt/gsoap-2.8/gsoap/stdsoap2.c stdsoap2.h ./
4、編寫Makefile
GSOAP_ROOT = /root/gsoap-2.8/gsoap
WSNAME = add
CC = g++ -g -DWITH_NONAMESPACES
INCLUDE = -I $(GSOAP_ROOT)
SERVER_OBJS =soapC.o stdsoap2.o soapServer.o $(WSNAME)server.o
CLIENT_OBJS =soapC.o stdsoap2.o soapClient.o $(WSNAME)client.o
all: server
server: $(SERVER_OBJS)
$(CC) $(INCLUDE) -o $(WSNAME)server $(SERVER_OBJS)
client: $(CLIENT_OBJS)
$(CC) $(INCLUDE) -o $(WSNAME)client $(CLIENT_OBJS)
clean:
rm -f *.o *.xml *.a *.wsdl *.nsmap soap* $(WSNAME)Stub.* $(WSNAME)server ns.xsd $(WSNAME)test
5、編譯
#make server
#make client
編譯生成服務端執行程序addserver和客戶端執行程序addclient
6、執行
(1)在終端上執行服務器程序
#./addserver 8888
終端打印出“Socket connection successful: master socket = 3”,那麼你的server已經在前臺run起來了
(2)新建另一終端執行客戶端程序
#./addclient http://localhost:8888 99 22
[root@localhost gsoap]# ./addclient http://localhost:8888 99 22
server is http://localhost:8888, num1 is 99, num2 is 22
99 + 22 = 121
server is http://localhost:8888, num1 is 99, num2 is 22
99 - 22 = 77
[root@localhost gsoap]#