發現gsoap的手冊,所寫的內容,和現行版本已經有落差。
在入門的階段,還搞不清楚東西南北的時候,沒有一個可行的例子,還真讓人不知如何向前推進。
範例1:
~/gsoap-2.8/gsoap/samples/hello
程式很簡單:
client端的程式-->helloclient.cpp
$ cat helloclient.cpp
#include "soapH.h"
#include "h.nsmap"
int main()
{
char *s;
soap_call_h__hello(soap_new(), "http://www.cs.fsu.edu/~engelen/hellolitserver.cgi", "", s);
return !printf("%s\n", s);
}
server端的程式
$ cat helloserver.cpp
#include "soapH.h"
#include "h.nsmap"
int main()
{ return soap_serve(soap_new()); }
int h__hello(struct soap *soap, char *&s) {
s = "Hello World!";
return SOAP_OK; }
README 上寫著,請server端的程式編譯成cgi的模式附在web server上
make 後,即可使用。
下**./helloclient**
回傳
Hello World!
make時發生了那些事??
$ ls -lt -r
total 1432
-rw-r--r-- 1 timloo timloo 537 2010-09-20 17:33 README.txt
-rw-r--r-- 1 timloo timloo 631 2010-09-20 17:33 Makefile.am
-rw-r--r-- 1 timloo timloo 166 2010-09-20 17:33 helloserver.cpp
-rw-r--r-- 1 timloo timloo 18 2010-09-20 17:33 hello.h
-rw-r--r-- 1 timloo timloo 182 2010-09-20 17:33 helloclient.cpp
-rw-r--r-- 1 timloo timloo 15803 2010-09-20 17:33 Makefile.in
-rw-r--r-- 1 timloo timloo 15451 2011-10-21 08:00 Makefile
-rw-r--r-- 1 timloo timloo 1640 2011-10-23 05:37 soapProxy.h
-rw-r--r-- 1 timloo timloo 2301 2011-10-23 05:37 soapObject.h
-rw-r--r-- 1 timloo timloo 974 2011-10-23 05:37 h.xsd
-rw-r--r-- 1 timloo timloo 2660 2011-10-23 05:37 h.wsdl
-rw-r--r-- 1 timloo timloo 535 2011-10-23 05:37 h.nsmap
-rw-r--r-- 1 timloo timloo 436 2011-10-23 05:37 h.hello.res.xml
-rw-r--r-- 1 timloo timloo 397 2011-10-23 05:37 h.hello.req.xml
-rw-r--r-- 1 timloo timloo 6427 2011-10-23 05:37 soapStub.h
-rw-r--r-- 1 timloo timloo 3250 2011-10-23 05:37 soapServer.cpp
-rw-r--r-- 1 timloo timloo 20503 2011-10-23 05:37 soapH.h
-rw-r--r-- 1 timloo timloo 2131 2011-10-23 05:37 soapClient.cpp
-rw-r--r-- 1 timloo timloo 54116 2011-10-23 05:37 soapC.cpp
-rw-r--r-- 1 timloo timloo 38392 2011-10-23 05:37 helloclient.o
-rw-r--r-- 1 timloo timloo 39528 2011-10-23 05:37 soapClient.o
-rw-r--r-- 1 timloo timloo 128044 2011-10-23 05:38 soapC.o
-rwxr-xr-x 1 timloo timloo 493770 2011-10-23 05:38 helloclient
-rw-r--r-- 1 timloo timloo 38228 2011-10-23 05:38 helloserver.o
-rw-r--r-- 1 timloo timloo 40584 2011-10-23 05:38 soapServer.o
-rwxr-xr-x 1 timloo timloo 494138 2011-10-23 05:38 helloserver
2011-10-23產生的檔案,都是gsoap2的工作具toolkit產生的。
小結: gsoap2 toolkit是一個程式碼產生器,讓使用者不必知道很多細節,就可以產生符合soap規範的web
service。client不必是瀏覽器,可以沒有GUI,只有指令列也可以。而server端可以是早期的
CGI 程式的型式。
如果之前寫過socket 程式,知道仍然要寫些send(),bind(),socketaddr(),叫用這些網路呼叫,而gsoap2 toolkit跳出來,幫你做掉這些事。
範例2:
~/gsoap-2.8/gsoap/samples/gmt
傳中央標準時間給呼叫者。
$ cat gmtclient.cpp
#include "soapH.h"
#include "t.nsmap"
int main() { time_t t; soap_call_t__gmt(soap_new(), "http://www.cs.fsu.edu/~engelen/gmtlitserver.cgi", "", &t); return printf("The current time is %s\n", ctime(&t)); }
timloo@ubuntu:~/gsoap-2.8/gsoap/samples/gmt$ cat gmtserver.cpp
#include "soapH.h"
#include "t.nsmap"
int main() { return soap_serve(soap_new()); } int t__gmt(struct soap *soap, time_t *t) { *t = time(0); return SOAP_OK; }
原作者的風格,就是一行寫到底,用分號來斷句,不換行。
make 後,
執行
$ ./gmtclient
The current time is Sun Oct 23 06:28:39 2011
web service,算是所謂的rpc,走http,call 遠端的function,再return回來本機。
**小結:**照之前所寫的,可用wsdl檔,來產生h檔,再從h檔來產生
stub,proxy,server等h, c檔。
範例3:
~/gsoap-2.8/gsoap/samples/roll
$ cat rollclient.cpp
#include "soapH.h"
#include "r.nsmap"
int main() { int d; soap_call_r__roll(soap_new(), "http://www.cs.fsu.edu/~engelen/rolllitserver.cgi", "", d); return !printf("%d\n", d); }
timloo@ubuntu:~/gsoap-2.8/gsoap/samples/roll$ cat rollserver.cpp
#include "soapH.h"
#include "r.nsmap"
int main() { return soap_serve(soap_new()); } int r__roll(struct soap *soap, int &r) { srand(time(0)); r = rand()%6+1; return SOAP_OK; }
make編譯,
執行結果:
timloo@ubuntu:~/gsoap-2.8/gsoap/samples/roll$ ./rollclient
6
timloo@ubuntu:~/gsoap-2.8/gsoap/samples/roll$ ./rollclient
5
timloo@ubuntu:~/gsoap-2.8/gsoap/samples/roll$ ./rollclient
6
timloo@ubuntu:~/gsoap-2.8/gsoap/samples/roll$ ./rollclient
1
timloo@ubuntu:~/gsoap-2.8/gsoap/samples/roll$ ./rollclient
1
timloo@ubuntu:~/gsoap-2.8/gsoap/samples/roll$ ./rollclient
3
用亂數產生,1~6的數字。
到這裏,可能會有疑問,一定要用CGI的寫法嗎?不會寫CGI這種老古董的東西這麼辦?
helloclient.cpp
#include "soapH.h"
#include "h.nsmap"
int main()
{
char *s;
soap_call_h__hello(soap_new(), "http://www.cs.fsu...(恕刪)
好帥的寫法 return !printf("%s\n", s);
推c++高手!!!!
kradark 參上