pstream_client.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*=============================================================================
  2. pstream_client.cpp
  3. ===============================================================================
  4. This is an example of a client that uses XML-RPC for C/C++
  5. (Xmlrpc-c).
  6. In particular, it uses the simple "packet stream" XML transport mechanism
  7. instead of HTTP as specified by XML-RPC (so this is not an XML-RPC
  8. client).
  9. You have to supply as Standard Input a stream (TCP) socket whose other
  10. end is hooked up to the RPC server. The 'socket_exec' program is a
  11. good way to arrange that.
  12. The sample program pstream_server.cpp is compatible with this client.
  13. Example:
  14. $ socketexec -connect -remote_host=localhost -remote_port=8080 \
  15. ./pstream_client
  16. =============================================================================*/
  17. #include <cassert>
  18. #include <cstdlib>
  19. #include <string>
  20. #include <iostream>
  21. #include <unistd.h>
  22. #include <signal.h>
  23. #include <xmlrpc-c/girerr.hpp>
  24. #include <xmlrpc-c/base.hpp>
  25. #include <xmlrpc-c/client.hpp>
  26. #include <xmlrpc-c/client_transport.hpp>
  27. using namespace std;
  28. int
  29. main(int argc, char **) {
  30. if (argc-1 > 0) {
  31. cerr << "This program has no arguments" << endl;
  32. exit(1);
  33. }
  34. // It's a good idea to disable SIGPIPE signals; if server closes his end
  35. // of the pipe/socket, we'd rather see a failure to send a call than
  36. // get killed by the OS.
  37. signal(SIGPIPE, SIG_IGN);
  38. try {
  39. xmlrpc_c::clientXmlTransport_pstream myTransport(
  40. xmlrpc_c::clientXmlTransport_pstream::constrOpt()
  41. .fd(STDIN_FILENO));
  42. xmlrpc_c::client_xml myClient(&myTransport);
  43. string const methodName("sample.add");
  44. xmlrpc_c::paramList sampleAddParms;
  45. sampleAddParms.add(xmlrpc_c::value_int(5));
  46. sampleAddParms.add(xmlrpc_c::value_int(7));
  47. xmlrpc_c::rpcPtr myRpcP(methodName, sampleAddParms);
  48. xmlrpc_c::carriageParm_pstream myCarriageParm;
  49. // Empty; transport doesn't need any information
  50. myRpcP->call(&myClient, &myCarriageParm);
  51. assert(myRpcP->isFinished());
  52. int const sum(xmlrpc_c::value_int(myRpcP->getResult()));
  53. // Assume the method returned an integer; throws error if not
  54. cout << "Result of RPC (sum of 5 and 7): " << sum << endl;
  55. } catch (exception const& e) {
  56. cerr << "Client threw error: " << e.what() << endl;
  57. } catch (...) {
  58. cerr << "Client threw unexpected error." << endl;
  59. }
  60. return 0;
  61. }