Author: Wei Qiong
From: Linux Inventory (http://www.linuxmine.com)
Contact: weiqiong # gmail.com
I. system environment 2
Ii. Example 2 of gsoap
3. Illustration 6
4. Notes 6
V. Reference Document 7
6. remarks 7
1. System Environment
Linux operating system kernel2.4.2, install gsoap2.6 to the directory/usr/local/gsoap
Ii. Example of gsoap
The following is a simple example to implement a WebService for addition operations. The specific function is to input num1 and num2 on the CLI side, and the server side returns the sum of num1 and num2.
1. First, we need to write a function declaration file to define the interface function NS _ add. The file name is add. h. The content is 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 MAKEFILE file to use the gsoapcpp2 tool from ADD. h generate some. XML file ,. c file and. h file. These files are automatically generated. The contents of makefile 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
# Overall goal
ALL: Server
$ {Wsname}. WSDL: $ {wsname}. h
$ (Gsoap_root)/soapcpp2-p $ (wsname)-I-n-C $ {wsname}. h
Stdsoap2.o: $ (gsoap_root)/stdsoap2.c
$ (CC)-C $?
# Generate the rule's. o file after compilation
$ (All_objs): %. O: %. c
$ (CC)-C $? $ (Include)
# Compile the server
Server: makefile $ {wsname}. WSDL $ {wsname} server. o $ (server_objs)
$ (CC) $ {wsname} server. o $ (server_objs)-o $ {wsname} Server
# Compile the client
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) proxy. * $ (wsname) object. * $ (wsname) serverlib. C $ (wsname) clientlib. C $ (wsname) server ns. XSD $ (wsname) test
3. Let's create a server and create the addserver. c file. The content is 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 <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 );
Add_serve (& add_soap); // specifies the server service.
Soap_end (& add_soap );
}
}
Return 0;
}
// The server-side implementation function is the same as the function declared in Add. h, but the current SOAP connection parameter is added.
Int NS _ add (struct soap * add_soap, int num1, int num2, int * sum)
{
* Sum = num1 + num2;
Return 0;
}
4. Let's run the server:
Shell> make
Shell>./addserver 8888
If the terminal prints "socket connection successful: Master socket = 3", your server is already running on the front-end and should be happy.
Open IE and type http: // The local IP Address: 8888. XML is displayed. The service is started and the terminal prints "socket connection successful: slave socket = 4 ", indicates that the Service receives a soap connection.
5. Let's write another client (this is just to encapsulate the soap client function. For details about the call, see addtest. c) and create the file addclient. C. The content is as follows:
# Include "addstub. H"
# Include "Add. nsmap"
/**
* Input parameter: SERVER: server address
* Num1, num2: the number to be added
* Output parameter: sum: result of adding num1 and num2
* Return value: 0 indicates success, and other values indicate failure.
*/
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 );
// This function is the main function called by the client, followed by several parameters and Add. H is declared in the same way. There are three more parameters. The function name is the interface function name NS _ add followed by 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. We finally write a client call program that can run and create the file addtest. C. The content is as follows:
# Include <stdio. h>
# Include <stdlib. h>
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. Let our client and server communicate
Shell> make Client
Shell>./addtest 7 8
Of course, your server should be still running, so that the output result is 7 + 8 = 15. Well, you have successfully completed the WebService written in your first C. Congratulations.
Iii. Illustration
4. Notes
1. The comments in front of the Add. h file cannot be deleted, which is the identifier to be recognized by soapcpp2.
2. the return value of an interface function can only be int, which is the result of a soap call. Generally, soap. error is used to determine the soap connection. This return value is not used.
3. The last parameter of the interface function is an outgoing parameter. If multiple parameters need to be transferred, You need to define a structure to encapsulate the return items.
4. Other. H files cannot be included in. H files, and may not take effect. You must directly declare them in this file when using some structures.
5. If the client call does not require a return value, the last parameter
V. References
1. gsoap Homepage
Http://gsoap2.sourceforge.net
2. Write makefile with me
Http://dev.csdn.net/develop/article/20/20025.shtm
3. Web Services: A technical introduction (Mechanical Industry Press)
Vi. Remarks
3 files and an env directory under the/usr/local/gsoap directory of 192.168.18.233 and 192.168.18.234 are not compiled and installed, it is directly copied after compilation in other places (the wsdl2h tool and other files are included in the actual compilation results, however, the three files and the Env directory are used in actual development ). Due to the rush of time, I have no time to study compilation issues. For details, refer to document 1.
In the/home/weiqiong/soap/sample directory of 192.168.18.233 and the/tmp/soap/sample directory of 192.168.18.234, there are examples of addition operations described in this article.