⽤C语⾔开发webservice
写在转载之前:
在⽤C语⾔开发webservice时,⾸先建⽴⼀个⼤的框架,然后再进⾏开发,这样在出现问题的时候,⾃⼰⼼⾥才有底,鉴于个⼈⽐较懒,不喜欢动笔,所以⽂章的内容就采⽤转载的⽅式!
1 在进⾏开发前需要明⽩的⼏个概念:
(1) 什么是webservice?
(2) 什么是soap协议?
(3) 什么是wsdl?
2 开发借助的⼯具:gsoap
3 利⽤gsoap快速开发⼀个简单的webservice接⼝的步骤如下:
(1) 根据wsdl⽣成.h⽂件--wsdl2h
(2) 根据.h⽂件⽣成⽤于底层通信的服务端和客户端的c语⾔代码--soapcpp2
(3) 快速查看服务端代码的结构,明确我们需要填充的代码
(4) 快速查看客户端代码的结构,明确我们需要填充的代码
下⽂是转载的内容:
gSOAP是⼀个夸平台的,⽤于开发Web Service服务端和客户端的⼯具,在Windows、Linux、MAC OS和UNIX下使⽤C和C++语⾔编码,集合了SSL功能。
⾸先查看gsoap的User's Guide,基本就能对gsoap有个全⾯的了解,通过阅读Sample⾥的例⼦程序深⼊。然后搜索⽹上其它⼀些⽂章,⽐如:
gSOAP简单多线程服务器程序
纯c gSoap实现WebService
下⽂阐述在VC下⽤gsoap编写webService和客户端程序:
⼀ 服务器端
1.⾸先编写 add.h⽂件:
1//
2//gsoap ns service namespace: localhost/add.wsdl
3//gsoap ns service location: localhost
4//
5//
6//gsoap ns schema namespace: urn:add
7
8int
9
2.⽤gsoap/bin⽬录下的程序,⽣成⼀些⽂件。可以把拷贝到⼀add.h⽬录下,⽤cmd执⾏ add.h就可以,在这个⽬录下会⾃动⽣成许多将来有⽤的⽂件,如add.namap,soapH.h,soapC.cpp,soapClient.cpp,soapServer.cpp等⽂件。可以带参数执
⾏,具体执⾏ -h查看。
3.新建⼀个win32控制台⼯程,加⼊wsock32.lib库,将刚才⽣成的那些⽂件添加到⼯程中。然后编写webserver.cpp主程序:
#include "add.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 <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;
}
//server端的实现函数与add.h中声明的函数相同,但是多了⼀个当前的soap连接的参数
int ns__add(struct soap *add_soap, int num1, int num2, int *sum)
{
*sum = num1 + num2;
return 0;
}
4. 编译这个程序,会提⽰错误,将gsoap_win32⽬录下stdsoap2.cpp,stdsoap2.h⽂件加⼊⼯程,重新编译如果还有错误,可能是你将add.h⽣成的⽂件添加⼊⼯程出错的原因。实际上在编写server程序时,⽆须带Client的那些⽂件,还有带Lib的⽂件也⽆须添加到⼯程中。再重新编译应该就没有问题了,启动4567端⼝,在ie中输⼊localhost:4567,如果显⽰xml页⾯,说明程序已经启动。
⼆ 对应的客户端
1。客户端程序代码如下:
#include <stdio.h>
#include <stdlib.h>
#include "soapH.h"
#include "add.nsmap"
int add(const char* server, int num1, int num2, int *sum);
int main(int argc, char **argv)
{
int result = -1;
char* server="localhost:4567";
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;
}
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 ()
{
printf("soap error:%d,%s,%s\n", , *soap_faultcode(&add_soap), *soap_faultstring(&add_soap) );
result = ;
}
soap_end(&add_soap);
soap_done(&add_soap);
return result;
}
2.客户端程序既可以新建⼀个新的win32控制台程序,将刚才⽣成的nsmap,soapH.h,soapClient.h等⽂件加⼊⼯程,编译既可。我是直接在原先⼯程中加⼊⼀客户端代码,将webserver.cpp⽂件移除,并且将soapServer.cpp等server端需要的⽂件移除,将soapClient.cpp等client端需要的cpp添加到⼯程,编译既可。
3.启动server程序,F5客户端程序,经测试正常。
三 遇到的问题
1.server端可以编译成CGI⽅式执⾏,⽽并不是绑定到某个端⼝,这种⽅式我没有实践。
if (argc < 2) // no args: assume this is a CGI application
{
soap_serve(&soap); // serve request, one thread, CGI style
soap_destroy(&soap); // dealloc C++ data
soap_end(&soap); // dealloc data and clean up
}
2.在编译服务器及客户端程序时⼀开始对add.h⽣成的⽂件添加到⼯程,经常出现问题,需要⾃⼰不调试。特别是链接时段,server/client要与其⽣成的⽂件相对应,server调⽤⽣成的soapserver.cpp,client调⽤⽣成的soapclient.cpp⽂件。
⼀ gSOAP需要的头⽂件:
////////gsoap ns service namespace: 127.0.0.1//gsoap ns service location: 127.0.0.1//gsoap ns schema namespace: urn:calc int int int
int ns__div(double a, double b, double *result);
int ns__pow(double a, double b, double *result);
⼆ 多线程服务器关键代码
#include
#include "calc.nsmap"
#include "soapH.h"
/**//
///宏与全局变量的定义
#define BACKLOG (100)
#define MAX_THR (10)
#define MAX_QUEUE (1000)
pthread_mutex_t queue_cs; //队列锁
pthread_cond_t queue_cv; //条件变量
SOAP_SOCKET queue[MAX_QUEUE]; //数组队列
int head =0, tail =0; //队列头队列尾初始化
/**///
//
void * process_queue(void *); //线程⼊⼝函数
int enqueue(SOAP_SOCKET); //⼊队列函数
SOAP_SOCKET dequeue(void); //出队列函数
/**///
//线程⼊⼝函数
void * process_queue(void * soap)
{
struct soap * tsoap = (struct soap *)soap;
for(;;)
{
tsoap->socket = dequeue();
if (!soap_valid_socket(tsoap->socket))
{
break;
}
soap_serve(tsoap);
soap_destroy(tsoap);
soap_end(tsoap);
}
return NULL;
}
//⼊队列操作
int enqueue(SOAP_SOCKET sock)
{
int status = SOAP_OK;mutex error
int next;
pthread_mutex_lock(&queue_cs);
next = tail +1;
if (next >= MAX_QUEUE)
next = 0;
if (next == head)
status = SOAP_EOM;
else
{
queue[tail] =sock;
tail = next;
}
pthread_cond_signal(&queue_cv);
pthread_mutex_unlock(&queue_cs);
return status;
}
//出队列操作
SOAP_SOCKET dequeue()
{
SOAP_SOCKET sock;
pthread_mutex_lock(&queue_cs);
while (head == tail )
{
pthread_cond_wait(&queue_cv,&queue_cs);
}
sock = queue[head++];
if (head >= MAX_QUEUE)
{
head =0;
}
pthread_mutex_unlock(&queue_cs);
return sock;
}
/**///具体服务⽅法////
//加法的实现
int ns__add(struct soap *soap, double a, double b, double *result)
{
*result = a + b;
return SOAP_OK;
}
//减法的实现
int ns__sub(struct soap *soap, double a, double b, double *result)
{
*result = a - b;
return SOAP_OK;
}
//乘法的实现
int ns__mul(struct soap *soap, double a, double b, double *result)
{
*result = a * b;
return SOAP_OK;
}
//除法的实现
int ns__div(struct soap *soap, double a, double b, double *result)
{
if (b)
*result = a / b;
else
{
char *s = (char*)soap_malloc(soap, 1024);
sprintf(s, "Can't">/">Can't divide %f by %f", a, b);
return soap_sender_fault(soap, "Division by zero", s);
}
return SOAP_OK;
}
//乘⽅的实现
int ns__pow(struct soap *soap, double a, double b, double *result)
{
*result = pow(a, b);
if (soap_errno == EDOM) /**//* soap_errno 和errorno类似, 但是和widnows兼容 */
{
char *s = (char*)soap_malloc(soap, 1024);
sprintf(s, "Can't take the power of %f to %f", a, b);
sprintf(s, "Can't">/">Can't take power of %f to %f", a, b);
return soap_sender_fault(soap, "Power function domain error", s);
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论