这两天,接到一个项目,需要在linux程序中调用远程的web应用,通过soap协议。开始上网查了下资料,发现了gsoap库这个好东东^_^。继续在网上搜索例子代码,发现基本都不可编译通过,于是便一边学习一边写了这个最简单的例子,希望对后来者起到一点帮助。
- 对gsoap的简单介绍,请自己参阅http://gsoap2.sourceforge.net/
下载相应的包,主要有2个工具和源代码:
wsdl2h -o outfile.h infile.wsdl 实现wsdl文件到h文件的数据映射
soapcpp2 -c outfile.h生成相应的底层通信stub,strech程序
- 下面这个简单的例子实现的是在客户端输入2个数字,然后远程调用服务端的加法函数,最后返回结果给客户端。
在这里我们不需要wsdl的文件,可以直接从.h文件来生成代码。我们定义一个函数声明文件,用来定义接口函数,名称为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
- //gsoap ns2 service port:http://websrv.cs.fsu.edu/~engelen/addserver.cgi
- //gsoap ns2 service namespace: urn:add
- //gsoap ns2 service transport: http://schemas.xmlsoap.org/soap/http
- //gsoap ns2 service method-style: add rpc
- //gsoap ns2 service method-encoding:
- add http://schemas.xmlsoap.org/soap/encoding/
- //gsoap ns2 service method-action: add ""
- int ns2__add( int num1, int num2, int* sum );
然后我们执行soapcpp2 -c add.h,自动生成一些远程调用需要的文件。
接下来我们写一个服务端,创建文件addserver.c
- #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;
- }
我们接着写客户端,文件addclient.c
- #include "soapStub.h"
- #include "add.nsmap"
- 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;
- }
最后写一个测试代码,addtest.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int add(const char *server, int num1, int num2, int *sum);
- 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, ∑);
- if (result != 0) {
- printf("soap error, errcode=%d/n", result);
- } else {
- printf("%d + %d = %d/n", num1, num2, sum);
- }
- return 0;
- }
到此为止,我们自己的代码已经编写完毕,现在我们来编译服务端和客户端
注意:编译的时候我们需要gsoap包里的源代码文件,把stdsoap2.c和stdsoap2.h文件拷贝到当前目录
我们写一个Makefile文件:
- GSOAP_ROOT = /root/gsoap-2.7/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 $(WSNAME)test.o
- all: server
- server: $(SERVER_OBJS)
- $(CC) $(INCLUDE) -o $(WSNAME)server $(SERVER_OBJS)
- client: $(CLIENT_OBJS)
- $(CC) $(INCLUDE) -o $(WSNAME)test $(CLIENT_OBJS)
- cl:
- rm -f *.o *.xml *.a *.wsdl *.nsmap soapH.h $(WSNAME)Stub.* $(WSNAME)server ns.xsd $(WSNAME)test
然后我们执行make,即可生产addserver程序;make client,生成addtest程序。
让server跑起来,执行./addserver 6666
终端打印出“Socket connection successful: master socket = 3”,那么你的server已经在前台run起来了;
运行客户端,./addtest ip:port num1 num2,返回加法的结果。
OK,一个最简单的soap调用的例子完成了,进深一步的学习请参考http://gsoap2.sourceforge.net/