Linux下⽤gSOAP开发WebService服务端和客户端程序
⽹上本有⼀篇流传甚⼴的C版本的,我参考来实现,发现有不少问题,现在根据⾃⼰的开发经验将其修改,使⽤⽆误;另外,补充同样功能的C++版本,我想这个应该更有⽤,因为能⽤C++,当然好过受限于C。
1、gSOAP的安装:
简单安装:
configure --prefix=/usr/local/gSOAP
make
make install
为了后⾯的C和C++版本的服务和客户端的开发能够脱离源代码⽬录,最好还要如下处理:
(1)将源代码⽬录下gsoap⼦⽬录中的import⽬录拷贝到gSOAP⽬录下来;
(2)gSOAP⽬录下建⼀个env⽬录,将gsoap/samples/link下的所有⽂件拷贝过来,并且⽣成envC.o(
C版本要⽤到),⽅法是:
/usr/local/gSOAP/bin/soapcpp2 -penv -c env.h
g++ -c -I /usr/local/gSOAP/include envC.c
(3)gSOAP⽬录下建⼀个src⽬录,将将源代码⽬录下gsoap⼦⽬录中的stdsoap*.*拷贝过来。调用webservice服务
2、功能说明:
要开发的Web Service功能⾮常简单,就是⼀个add函数,将两个参数相加,返回其和。
3、C版本的程序:
(1)头⽂件:SmsWBS.h
int ns__add(int num1, int num2, int *sum);
这个头⽂件需要注意的是,前⾯的 // 部分是有意义的,可以在上⾯修改,如果完全去掉,将会导致⽣成的中间⽂件不同,由此会引起要修改Makefile⽂件
(2)Makefile⽂件:
⽐较关键,如果头⽂件中的 // 部分有修改,要检查是否要修改Makefile⽂件
GSOAP_ROOT=/usr/local/gSOAP
WSNAME0=soap
WSNAME=SmsWBS
CC=g++ -g -DWITH_NONAMESPACES
INCLUDE=-I $(GSOAP_ROOT)/include
SERVER_OBJS=$(WSNAME0)C.o $(WSNAME0)Server.o stdsoap2.o
CLIENT_OBJS=$(GSOAP_ROOT)/env/envC.o $(WSNAME0)ClientLib.o stdsoap2.o
ALL_OBJS=${WSNAME}server.o $(WSNAME0)C.o $(WSNAME0)Server.o ${WSNAME}test.o $(WSNAME0)ClientLib.o
#GSOAP_SRC=/usr/local/gsoap-2.7/gsoap
all:server
${WSNAME}.wsdl:${WSNAME}.h
$(GSOAP_ROOT)/bin/soapcpp2 -c $(GSOAP_ROOT)/import ${WSNAME}.h
stdsoap2.o:$(GSOAP_ROOT)/src/stdsoap2.c
$(CC) -c $? $(INCLUDE)
$(ALL_OBJS):%.o:%.c
$(CC) -c $? $(INCLUDE)
server:Makefile ${WSNAME}.wsdl ${WSNAME}server.o $(SERVER_OBJS)
$(CC) ${WSNAME}server.o $(SERVER_OBJS) -o ${WSNAME}server
client:Makefile ${WSNAME}.wsdl ${WSNAME}test.c $(ALL_OBJS) stdsoap2.o
$(CC) ${WSNAME}test.o $(CLIENT_OBJS) -o ${WSNAME}test
clean:
rm -f *.o *.xml *.a *.wsdl *.nsmap $(WSNAME0)H.h $(WSNAME0)C.c $(WSNAME0)Server.c $(WSNAME0)Client.c $(WSNAME0)Stub.* $(WSNAME)$(WSNAME)Proxy.* $(WSNAME)$(WSNAME)Object.* $(WSNAME0)ServerLib.c $(WSNAME0)ClientLib.c $(WSNAME)server ns.xsd $(WSNAME)test
(3)服务端程序SmsWBSserver.c:
#include "soapH.h"
#include "SmsWBS.nsmap"
int main(int argc, char **argv)
{
int m, s; /* master and slave sockets */
struct soap SmsWBS_soap;
soap_init(&SmsWBS_soap);
soap_set_namespaces(&SmsWBS_soap, namespaces);
if (argc < 2)
{
printf("usage: %s <server_port> \n", argv[0]);
exit(1);
}
else
{
m = soap_bind(&SmsWBS_soap, NULL, atoi(argv[1]), 100);
if (m < 0)
{
soap_print_fault(&SmsWBS_soap, stderr);
exit(-1);
}
fprintf(stderr, "Socket connection successful: master socket = %d\n", m);
for (;;)
{
s = soap_accept(&SmsWBS_soap);
if (s < 0)
{
soap_print_fault(&SmsWBS_soap, stderr);
exit(-1);
}
fprintf(stderr, "Socket connection successful: slave socket = %d\n", s);
soap_serve(&SmsWBS_soap);
soap_end(&SmsWBS_soap);
}
}
return 0;
}
int ns__add(struct soap *add_soap, int num1, int num2, int *sum)
{
*sum = num1 + num2;
return 0;
}
(4)客户端程序SmsWBStest.c:
#include <stdio.h>
#include <stdlib.h>
#include "soapStub.h"
#include "SmsWBS.nsmap"
int add(const char *server, int num1, int num2, int *sum);
int add(const char *server, int num1, int num2, int *sum)
{
struct soap SmsWBS_soap;
int result = 0;
soap_init(&SmsWBS_soap);
soap_set_namespaces(&SmsWBS_soap, namespaces);
soap_call_ns__add(&SmsWBS_soap, server, "", num1, num2, sum);
if()
{
printf("soap error:%d, %s, %s ", , *soap_faultcode(&SmsWBS_soap), *soap_faultstring(&SmsWBS_soap));
result = ;
}
soap_end(&SmsWBS_soap);
soap_done(&SmsWBS_soap);
return result;
}
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);
if (result != 0)
{
printf("soap err, errcode = %d \n", result);
}
else
{
printf("%d + %d = %d \n", num1, num2, sum);
}
return 0;
}
(5)编译和运⾏:
前⾯都已经准备好了,现在只需要:
make ---得到服务端程序SmsWBSserver
make client ---得到客户端程序SmsWBStest
SmsWBSserver 9000 ----运⾏服务端程序
出来类似下⾯的显⽰就表⽰运⾏正常
Socket connection successful: master socket = 3
再运⾏客户端程序:
SmsWBStest 67 78
显⽰:
67 + 78 = 145
调⽤Web Service成功
续(⼆)
接(⼀)
4、C++版本的程序:
(1)头⽂件不变,还是SmsWBS.h
(2)Makefile⽂件:
GSOAP_ROOT=/usr/local/gSOAP
WSNAME0=soap
WSNAME=SmsWBS
CC=g++ -g -DWITH_NONAMESPACES
INCLUDE=-I $(GSOAP_ROOT)/include
SERVER_OBJS=$(WSNAME0)C.o $(WSNAME0)$(WSNAME)Service.o stdsoap2.o
CLIENT_OBJS=$(WSNAME0)$(WSNAME)Proxy.o $(WSNAME0)C.o stdsoap2.o
ALL_OBJS=${WSNAME}server.o $(WSNAME0)C.o $(WSNAME0)$(WSNAME)Service.o $(WSNAME0)$(WSNAME)Proxy.o ${WSNAME}test.o
#GSOAP_SRC=/usr/local/gsoap-2.7/gsoap
all:server
${WSNAME}.wsdl:${WSNAME}.h
$(GSOAP_ROOT)/bin/soapcpp2 -i $(GSOAP_ROOT)/import ${WSNAME}.h
stdsoap2.o:$(GSOAP_ROOT)/src/stdsoap2.cpp
$(CC) -c $? $(INCLUDE)
$(ALL_OBJS):%.o:%.cpp
$(CC) -c $? $(INCLUDE)
server:Makefile ${WSNAME}.wsdl ${WSNAME}server.o $(SERVER_OBJS)
$(CC) ${WSNAME}server.o $(SERVER_OBJS) -o ${WSNAME}server
client:Makefile ${WSNAME}.wsdl ${WSNAME}test.cpp $(ALL_OBJS) stdsoap2.o
$(CC) ${WSNAME}test.o $(CLIENT_OBJS) -o ${WSNAME}test
clean:
rm -f *.o *.xml *.a *.wsdl *.nsmap $(WSNAME0)H.h $(WSNAME0)C.cpp $(WSNAME0)Server.cpp $(WSNAME0)Stub.* $(WSNAME0)$(WSNAME)S
ervice.* $(WSNAME0)$(WSNAME)Proxy.* $(WSNAME0)$(WSNAME)Object.* $(WSNAME0)ServerLib.cpp $(WSNAME0)ClientLib.cpp $(WSNAME)server ns.x
sd $(WSNAME)test
(3)服务端程序SmsWBSserver.cpp:
#include "soapSmsWBSService.h"
#include "SmsWBS.nsmap"
int main(int argc, char **argv)
{
SmsWBSService sms;
if (argc < 2)
sms.serve(); /* serve as CGI application */
else
{
int port = atoi(argv[1]);
if (!port)
{
fprintf(stderr, "Usage: SmsWBSserver++ <port>\n");
exit(0);
}
/* run iterative server on port until fatal error */
if (sms.run(port))
{
sms.soap_stream_fault(std::cerr);
exit(-1);
}
}
return 0;
}
int SmsWBSService::add(int num1, int num2, int *sum)
{
*sum = num1 + num2;
return SOAP_OK;
}
(4)客户端程序SmsWBStest.cpp:
#include <stdio.h>
#include <stdlib.h>
#include "soapSmsWBSProxy.h"
#include "SmsWBS.nsmap"
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]);
SmsWBSProxy sms;
result = sms.add(num1, num2, &sum);
if (result != 0)
{
printf("soap err, errcode = %d \n", result);
}
else
{
printf("%d + %d = %d \n", num1, num2, sum);
}
return 0;
}
(5)编译运⾏,与C版本类似,只是服务端运⾏时没有提⽰信息续(三)
接(⼆)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论