Chinaunix首页 | 论坛 | 博客
  • 博客访问: 523142
  • 博文数量: 576
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 5020
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:47
文章分类

全部博文(576)

文章存档

2011年(1)

2008年(575)

我的朋友

分类:

2008-10-14 14:57:43

用 C 实现 WebService

作者:求是赤子

一、系统环境

Linux 操作系统 kernel2.4.2,安装 gsoap2.6 到目录/usr/local/gsoap

二、gSOAP 的简要使用例子

  下面是一个简单的例子,实现一个加法运算的 WebService,具体功能是客户端(client)输入 num1 和 num2,服务器端(server)返回 num1 和 num2 相加的结果 sum。

1、首先,我们需要做的是写一个函数声明文件,来定义接口函数 ns__add,文件名字为 add.h,内容如下:

//gsoap ns service name: add
//gsoap ns service namespace: 
//gsoap ns service location: 
//gsoap ns service executable: add.cgi
//gsoap ns service encoding: encoded
//gsoap ns schema namespace: urn:add

int ns__add( int num1, int num2, int* sum );

2、然后我们需要创建文件 Makefile,从而利用 gsoapcpp2 工具由 add.h 生成一些 .xml 文件、.c 文件和 .h 文件,这些文件均为自动生成,Makefile的内容如下:

GSOAP_ROOT=/usr/local/gsoap
WSNAME=add
CC=g++ -g -DWITH_NONAMESPACES
INCLUDE=-I $(GSOAP_ROOT)
SERVER_OBJS=$(WSNAME)C.o $(WSNAME)Server.o stdsoap2.o
CLIENT_OBJS=$(GSOAP_ROOT)/env/envC.o $(WSNAME)ClientLib.o stdsoap2.o
ALL_OBJS=${WSNAME}server.o $(WSNAME)C.o $(WSNAME)Server.o ${WSNAME}test.o ${WSNAME}client.o $(WSNAME)ClientLib.o

#总的目标
all:server

${WSNAME}.wsdl:${WSNAME}.h
$(GSOAP_ROOT)/soapcpp2 -p$(WSNAME) -i -n -c ${WSNAME}.h

stdsoap2.o:$(GSOAP_ROOT)/stdsoap2.c
$(CC) -c $?

#编译一样生成规则的.o文件
$(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}client.c ${WSNAME}test.c $(ALL_OBJS) stdsoap2.o
$(CC) ${WSNAME}test.o ${WSNAME}client.o $(CLIENT_OBJS) -o ${WSNAME}test

cl:
rm -f *.o *.xml *.a *.wsdl *.nsmap $(WSNAME)H.h $(WSNAME)C.c $(WSNAME)Server.c $(WSNAME)Client.c $(WSNAME)Stub.* $(WSNAME)$(WSNAME)Proxy.* $(WSNAME)$(WSNAME)Object.* $(WSNAME)ServerLib.c $(WSNAME)ClientLib.c $(WSNAME)server ns.xsd $(WSNAME)test

3、我们先来做一个 server 端,创建文件 addserver.c 文件,内容如下:

#include "addH.h"
#include "add.nsmap"

int main(int argc, char **argv)
{
	int m, s; /* master and slave sockets */
	struct soap add_soap;
	soap_init(&add_soap);
	soap_set_namespaces(&add_soap, add_namespaces);
	if (argc < 2)
	{
		printf("usage: %s  \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);
			add_serve(&add_soap);//该句说明该server的服务
			soap_end(&add_soap);
		}
	}
	return 0;
}
//server端的实现函数与add.h中声明的函数相同,但是多了一个当前的soap连接的参数
int ns__add(struct soap *add_soap, int num1, int num2, int *sum)
{
	*sum = num1 + num2;
	return 0;
}		

4、让我们的server跑起来吧:

shell>make
shell>./addserver 8888

  如果终端打印出“Socket connection successful: master socket = 3”,那么你的 server 已经在前台 run 起来了,应该是值得高兴的!打开IE,键入,显示XML,服务已...〕Socket connection successful: slave socket = 4”,表示服务接收到了一次soap的连接。

5、让我们再来写个客户端(这个只是将soap的客户端函数封装一下,具体的调用参见下面的addtest.c),创建文件addclient.c,内容如下:

#include "addStub.h"
#include "add.nsmap"
/**
* 传入参数:server:server的地址
* num1,num2:需要相加的数
* 传出参数:sum:num1和num2相加的结果
* 返回值: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, add_namespaces);
	
	// 该函数是客户端调用的主要函数,后面几个参数和add.h中声明的一样,前面多了3个参数,
	// 函数名是接口函数名ns__add前面加上soap_call_
	soap_call_ns__add( &add_soap, server, "", num1, num2, sum );
	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;
}

6、我们最终写一个可以运行的客户端调用程序,创建文件addtest.c,内容如下:

#include 
#include 

int add(const char* server, int num1, int num2, int *sum);

int main(int argc, char **argv) 
{
	int result = -1;
	char* 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);
	if (result != 0)
	{
		printf("soap err,errcode = %d\n", result);
	}
	else
	{
		printf("%d+%d=%d\n", num1, num2, sum );
	}
	return 0;
}

7、让我们的client端和server端通讯

shell>make client
shell>./addtest 7 8

当然,你的server应该还在run,这样得到输出结果7+8=15,好了,你成功完成了你的第一个C写的 WebService,恭喜。

三、要注意的问题

  1. add.h文件前面的几句注释不能删除,为soapcpp2需要识别的标志;
  2. 接口函数的返回值只能是int,是soap调用的结果,一般通过soap.error来判断soap的连接情况,这个返回值没有用到;
  3. 接口函数的最后一个参数为传出参数,如果需要传出多个参数,需要自己定义一个结构将返回项封装;
  4. 在.h文件中不能include别的.h文件,可能不能生效,需要用到某些结构的时候需要在该文件中直接声明;
  5. 如果客户端的调用不需要返回值,那么最后一个参数;

五、参考文档

  • gsoap 主页
  • 跟我一起写 Makefile
  • Web Services: A Technical Introduction(机械工业出版社)

--------------------next---------------------

阅读(268) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~