1 /* A simple synchronous XML-RPC client program written in C, as an example of
2 an Xmlrpc-c client. This invokes the sample.add procedure that the
3 Xmlrpc-c example xmlrpc_sample_add_server.c server provides. I.e. it adds
4 two numbers together, the hard way.
5
6 This sends the RPC to the server running on the local system ("localhost"),
7 HTTP Port 8080.
8
9 This program uses the Xmlrpc-c global client, which uses the default
10 client XML transport.
11 */
12
13 #include <stdlib.h>
14 #include <stdio.h>
15
16 #include <xmlrpc-c/base.h>
17 #include <xmlrpc-c/client.h>
18
19 #include "config.h" /* information about this build environment */
20
21 #define NAME "Xmlrpc-c Test Client"
22 #define VERSION "1.0"
23
24 static void
dieIfFaultOccurred(xmlrpc_env * const envP)25 dieIfFaultOccurred (xmlrpc_env * const envP) {
26 if (envP->fault_occurred) {
27 fprintf(stderr, "ERROR: %s (%d)\n",
28 envP->fault_string, envP->fault_code);
29 exit(1);
30 }
31 }
32
33
34
35 int
main(int const argc,const char ** const argv)36 main(int const argc,
37 const char ** const argv) {
38
39 xmlrpc_env env;
40 xmlrpc_value * resultP;
41 xmlrpc_int32 sum;
42 const char * const serverUrl = "http://localhost:8080/RPC2";
43 const char * const methodName = "sample.add";
44
45 if (argc-1 > 0) {
46 fprintf(stderr, "This program has no arguments\n");
47 exit(1);
48 }
49
50 /* Initialize our error-handling environment. */
51 xmlrpc_env_init(&env);
52
53 /* Create the global XML-RPC client object. */
54 xmlrpc_client_init2(&env, XMLRPC_CLIENT_NO_FLAGS, NAME, VERSION, NULL, 0);
55 dieIfFaultOccurred(&env);
56
57 printf("Making XMLRPC call to server url '%s' method '%s' "
58 "to request the sum "
59 "of 5 and 7...\n", serverUrl, methodName);
60
61 /* Make the remote procedure call */
62 resultP = xmlrpc_client_call(&env, serverUrl, methodName,
63 "(ii)", (xmlrpc_int32) 5, (xmlrpc_int32) 7);
64 dieIfFaultOccurred(&env);
65
66 /* Get our sum and print it out. */
67 xmlrpc_read_int(&env, resultP, &sum);
68 dieIfFaultOccurred(&env);
69 printf("The sum is %d\n", sum);
70
71 /* Dispose of our result value. */
72 xmlrpc_DECREF(resultP);
73
74 /* Clean up our error-handling environment. */
75 xmlrpc_env_clean(&env);
76
77 /* Shutdown our XML-RPC client library. */
78 xmlrpc_client_cleanup();
79
80 return 0;
81 }
82
83
84
85