2
0

xmlrpc_sample_add_client.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. This sends the RPC to the server running on the local system ("localhost"),
  6. HTTP Port 8080.
  7. */
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <xmlrpc-c/base.h>
  11. #include <xmlrpc-c/client.h>
  12. #include "config.h" /* information about this build environment */
  13. #define NAME "Xmlrpc-c Test Client"
  14. #define VERSION "1.0"
  15. static void
  16. dieIfFaultOccurred (xmlrpc_env * const envP) {
  17. if (envP->fault_occurred) {
  18. fprintf(stderr, "ERROR: %s (%d)\n",
  19. envP->fault_string, envP->fault_code);
  20. exit(1);
  21. }
  22. }
  23. int
  24. main(int const argc,
  25. const char ** const argv) {
  26. xmlrpc_env env;
  27. xmlrpc_value * resultP;
  28. xmlrpc_int32 sum;
  29. const char * const serverUrl = "http://localhost:8080/RPC2";
  30. const char * const methodName = "sample.add";
  31. if (argc-1 > 0) {
  32. fprintf(stderr, "This program has no arguments\n");
  33. exit(1);
  34. }
  35. /* Initialize our error-handling environment. */
  36. xmlrpc_env_init(&env);
  37. /* Start up our XML-RPC client library. */
  38. xmlrpc_client_init2(&env, XMLRPC_CLIENT_NO_FLAGS, NAME, VERSION, NULL, 0);
  39. dieIfFaultOccurred(&env);
  40. printf("Making XMLRPC call to server url '%s' method '%s' "
  41. "to request the sum "
  42. "of 5 and 7...\n", serverUrl, methodName);
  43. /* Make the remote procedure call */
  44. resultP = xmlrpc_client_call(&env, serverUrl, methodName,
  45. "(ii)", (xmlrpc_int32) 5, (xmlrpc_int32) 7);
  46. dieIfFaultOccurred(&env);
  47. /* Get our sum and print it out. */
  48. xmlrpc_read_int(&env, resultP, &sum);
  49. dieIfFaultOccurred(&env);
  50. printf("The sum is %d\n", sum);
  51. /* Dispose of our result value. */
  52. xmlrpc_DECREF(resultP);
  53. /* Clean up our error-handling environment. */
  54. xmlrpc_env_clean(&env);
  55. /* Shutdown our XML-RPC client library. */
  56. xmlrpc_client_cleanup();
  57. return 0;
  58. }