Implement WebService with C

Source: Internet
Author: User

First, the system environment

Linux os kernel2.4.2, installing gsoap2.6 to directory/usr/local/gsoap

Ii. examples of the use of gSOAP

The following is a simple example that implements the WebService of an addition operation, which is the client input num1 and num2, and the server side returns the result sum of NUM1 and num2 addition.
1, first of all, we need to do is to write a function declaration file, to define the interface function Ns__add, the file name is Add.h, the contents are as follows:

//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, then we need to create the file Makefile, thus using the GSOAPCPP2 tool generated by add.h some. xml files,. c files, and. h files, all of which are automatically generated, and the Makefile contents are as follows:

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 File
$ (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, we first to do a server-side, the creation of file Addserver.c file, the contents are as follows:

#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 (&AMP;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 (&AMP;ADD_SOAP)://This sentence describes the service of the server
Soap_end (&AMP;ADD_SOAP);
}
}
return 0;
}
The server-side implementation function is the same as the function declared in Add.h, but there is one more parameter to the current SOAP connection
int Ns__add (struct soap *add_soap, int num1, int num2, int *sum)
{
*sum = Num1 + num2;
return 0;
}

4, let us run the server:

shell>make
shell>./addserver 8888

If the terminal prints "socket connection successful:master socket = 3", then your server has been in the foreground run up, it should be happy! Open IE, type http://localhost:8888, display XML, service has ...)socket connection successful:slave Socket = 4 ", which indicates that the service received a SOAP connection.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.