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