XML-RPC is yet another method of implementing remote procedure calls. It used XML over HTTP to transmit data. In my past live working at TLO I used XML-RPC-C library to implement communication between cluster nodes and a cluster management system. I thought the library was well designed and easy to use so I wanted to introduce you to it.
Below is a simple client and server implementation using the XML-RPC-C library. The server implements one RPC that accepts one string parameter and returns one string. The client makes the call to the server saying hello and prints the reply. The code is easy to read and does not need any further explanation 🙂
The client. xmlrpc_c.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> #include <string> #include <xmlrpc-c/girerr.hpp> #include <xmlrpc-c/base.hpp> #include <xmlrpc-c/client_simple.hpp> using namespace std; int main(int argc, char** argv) { string serverUrl("http://localhost:8080/RPC2"); string methodName("hello"); xmlrpc_c::clientSimple client; xmlrpc_c::value result; client.call(serverUrl, methodName, "s", &result, "XMLRPC client says hello!"); auto reply = xmlrpc_c::value_string(result); cout << static_cast<string>(reply) << endl; return 1; } |
The server. xmlrpc_s.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <iostream> #include <string> #include <stdexcept> #include <xmlrpc-c/base.hpp> #include <xmlrpc-c/registry.hpp> #include <xmlrpc-c/server_abyss.hpp> using namespace std; class hello : public xmlrpc_c::method { public: void execute(const xmlrpc_c::paramList& params, xmlrpc_c::value* retval) { string msg(params.getString(0)); params.verifyEnd(1); cout << msg << endl; *retval = xmlrpc_c::value_string("XMLRPC server says hello!"); } }; int main(int argc, char** argv) { xmlrpc_c::registry registry; registry.addMethod("hello", new hello); xmlrpc_c::serverAbyss server(xmlrpc_c::serverAbyss::constrOpt().registryP(®istry).portNumber(8080)); server.run(); return 1; } |
typo in client hellp –> hello
thanks 🙂
Just a few nitpicks on the code:
Arguments to
main
are declared but not used.
return 1
;
for
main
is needlessly using a not generally supported value, 1. The generally supported
main
return values are
0
,
EXIT_SUCCESS
, which is usually defined as
0
, and
EXIT_FAILURE
, which is usually defined as
1
. Reportedly, in OpenVMS return value 1 denotes *success*, while in Unix (including MacOS) and Windows it denotes *failure*. Just remove that
return
. There is a default for
main`, namely, that it returns 0, denoting success, by default.
So you’re saying I shouldn’t name unused
main
arguments, and let it return by default? Like this:
Yep.
Use pre tags for code blocks, and code tags for inline code.
#include "example_inline.h"
btw, i though the only standard compliant signature for main was int main(int, char**). the one without any parameters is non standard. am i wrong?
Never mind; int main() is standard compliant. I will update my blog post.
https://en.cppreference.com/w/c/language/main_function
Well, no, no arguments at all.