ºìÁªLinuxÃÅ»§
Linux°ïÖú

ÓÃCʵÏÖWebService

·¢²¼Ê±¼ä:2005-11-24 00:57:35À´Ô´:ºìÁª×÷Õß:·çÓêÎÞ×è
תÎÄ

Ò»£®ÏµÍ³»·¾³ 2
¶þ£®gSOAPµÄ¼òҪʹÓÃÀý×Ó 2
Èý£®Í¼Ê¾ËµÃ÷ 6
ËÄ£®Òª×¢ÒâµÄÎÊÌâ 6
Î壮²Î¿¼Îĵµ 7
Áù£®±¸×¢ 7

Ò»£®ÏµÍ³»·¾³
linux²Ù×÷ϵͳkernel2.4.2£¬°²×°gsoap2.6µ½Ä¿Â¼/usr/local/gsoap
¶þ£®gSOAPµÄ¼òҪʹÓÃÀý×Ó
ÏÂÃæÊÇÒ»¸ö¼òµ¥µÄÀý×Ó£¬ÊµÏÖÒ»¸ö¼Ó·¨ÔËËãµÄWebService£¬¾ßÌ幦ÄÜÊÇcli¶ËÊäÈënum1ºÍnum2£¬server¶Ë·µ»ØÒ»¸önum1ºÍnum2Ïà¼ÓµÄ½á¹ûsum¡£

1£® Ê×ÏÈ£¬ÎÒÃÇÐèÒª×öµÄÊÇдһ¸öº¯ÊýÉùÃ÷Îļþ£¬À´¶¨Òå½Ó¿Úº¯Êýns__add£¬ÎļþÃû×ÖΪadd.h£¬ÄÚÈÝÈçÏ£º

//gsoap ns service name: add
//gsoap ns service namespace: http://mail.263.net/add.wsdl
//gsoap ns service location: http://mail.263.net
//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£¬¼üÈëhttp://±¾»úIP:8888£¬ÏÔʾ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="http://localhost:8888";
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£® Èç¹û¿Í»§¶ËµÄµ÷Óò»ÐèÒª·µ»ØÖµ£¬ÄÇô×îºóÒ»¸ö²ÎÊý
Î壮²Î¿¼Îĵµ
1£®gsoapÖ÷Ò³
http://gsoap2.sourceforge.net

2£®¸úÎÒÒ»ÆðдMakefile
http://dev.csdn.net/develop/article/20/20025.shtm

3£®Web Services£º A Technical Introduction£¨»úе¹¤Òµ³ö°æÉ磩
Áù£®±¸×¢
192.168.18.233ºÍ192.168.18.234µÄ/usr/local/gsoapĿ¼ÏµÄ3¸öÐèÒªµÄÎļþ¼°Ò»¸öenvĿ¼£¬²»ÊDZàÒë°²×°µÄ£¬ÊÇÔÚ±ðµÄµØ·½±àÒëºÃÁËÖ±½Ócopy¹ýÀ´µÄ£¨Êµ¼Ê±àÒë½á¹ûÖл¹ÓÐwsdl2h¹¤¾ß¼°ÆäËûһЩÎļþ£¬µ«ÊÇÎÒÃǵÄʵ¼Ê¿ª·¢ÖÐÖ»ÊÇÓõ½ÁËÕâ3¸öÎļþ¼°envĿ¼£©¡£ÒòΪʱ¼ä²Ö´Ù£¬±¾ÈË»¹Ã»ÓÐʱ¼äÑо¿±àÒëµÄÎÊÌ⣬Ïà¹Øϸ½Ú¿ÉÒԲ鿴²Î¿¼Îĵµ1¡£
ÔÚ192.168.18.233µÄ/home/weiqiong/soap/sampleĿ¼Ï¼°192.168.18.234µÄ/tmp/soap/sampleĿ¼ÏÂÓб¾ÎĽ²µ½µÄ¼Ó·¨ÔËËãµÄÀý×Ó¡£

[ Last edited by ·çÓêÎÞ×è on 2005-11-24 at 00:58 ]
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 2 ÌõÆÀÂÛ

  1. ÓÚ 2012-03-27 10:17:59·¢±í:

    Õý³õ²½µÄѧϰ±àдÕâ¸ö£¬Ð»Ð»ÄãµÄÎÄÕ£¬ÕýºÃ¿ÉÒÔѧϰ¡£

  2. yo ÓÚ 2005-12-03 19:13:42·¢±í:

    ºÃ£¬Ö§³ÖÒ»¸ö