1. gsoap的好處就不用說了:百度百科
2. gsoap的下載地址:項目地址,目前我使用的是2.8.15版本
3. 開發環境:Ubuntu13.10
4. 具體操作步驟(以簡單相加為例):
1) 編寫add.h(頭文件)
//gsoap ns service name: calc //gsoap ns service protocol: SOAP //gsoap ns service style: rpc //gsoap ns service encoding: encoded //gsoap ns service namespace: http://localhost:8888 //gsoap ns service location: http://localhost:8888 //gsoap ns service port: http://localhost:8888 int ns__add( int num1, int num2, int* sum );
2) 編寫addserver.c(服務器)
#include "soapH.h" #include "calc.nsmap" /* 與add.h的第一行命名空間(ns)有關 */ #include <stdio.h> #include <stdlib.h> int main (int argc, char *argv[]) { int m, s; struct soap add_soap; soap_init (&add_soap); 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); //該句說明該server的服務 soap_end (&add_soap); } } return 0; } int ns__add (struct soap *add_soap, int num1, int num2, int *sum) { *sum = num1 + num2; return 0; }
3) 編寫addclient.c(客戶端)
#include "soapH.h" #include "calc.nsmap" /* 與add.h的第一行命名空間(ns)有關 */ #include <stdio.h> #include <stdlib.h> int add (const char *server, int num1, int num2, int *sum); int main (int argc, char **argv) { int result = -1; char *server = "http://localhost:4567"; /* 定義server */ int num1 = 0; int num2 = 0; int sum = 0; if (argc < 3) /* 判斷傳入參數 */ { printf ("usage: %s num1 num2 \n", argv[0]); exit (0); } num1 = atoi (argv[1]); num2 = atoi (argv[2]); result = add (server, num1, num2, &sum); /* 執行add() */ if (result != 0) /* 輸出result */ { printf ("soap err,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; /* 創建add_soap()結構體 */ int result = 0; soap_init (&add_soap); /* soap_init()*/ soap_call_ns__add (&add_soap, server, "", num1, num2, sum); /* 調用soap_call_ns_add() */ 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; }
4) 編寫Makefile文件
# this is a Makefile to build client and server # please setting the GSOAP_ROOT first. # build procedure: # step1: make soap # step2: make all OBJ_NS := calc OBJ_NAME := add GSOAP_ROOT := /home/scue/work/gsoap_2.8.15/gsoap INCLUDE := -I$(GSOAP_ROOT) CC := clang GCC := clang #CC := arm-linux-gcc #GCC := arm-linux-gcc OBJ_SERVER := soapC.o stdsoap2.o soapServer.o $(OBJ_NAME)server.o OBJ_CLIENT := soapC.o stdsoap2.o soapClient.o $(OBJ_NAME)client.o all: server client server: $(OBJ_SERVER) $(CC) $(INCLUDE) $^ -o $@ client: $(OBJ_CLIENT) $(CC) $(INCLUDE) $^ -o $@ .PHONY:soap soap: @cp -v $(GSOAP_ROOT)/stdsoap2.* . @$(GSOAP_ROOT)/bin/linux386/soapcpp2 -c $(OBJ_NAME).h # -c 表示生成c文件 .PHONY:clean clean: rm -f server client *.o distclean: rm -f server client *.o ns* soap* *.xml *.nsmap *.wsdl stdsoap2.*
5) 編譯及驗證
make soap && make ./server 4567 ./client 2 3 #將會返回5,也可以直接在瀏覽器中輸入http://localhost:4567進行驗證
參考文章:
