Important: Please read the Qt Code of Conduct -

Lightweight RPC

  • I was searching for a small RPC framework and found "qjsonrpc...": I like it because there are no other dependencies, just qt. The interfaces dont't need to be defined in another language, they are written in plain c++ and qt.
    Here is an example taken from a "blog...":
    The server:
    class TestService : public QJsonRpcService {
    Q_CLASSINFO("serviceName", "service")
    TestService(QObject *parent = 0) : QJsonRpcService(parent) {}
    public Q_SLOTS:
    QString currentTime() { return QTime::currentTime().toString(); }

    int main(int argc, char **argv) {
    QCoreApplication app(argc, argv);
    QJsonRpcLocalServiceProvider rpcServer;
    rpcServer.addService(new TestService);
    if (!rpcServer.listen("/tmp/testservice")) {
    qDebug() << "can't start local server: " << rpcServer.errorString();
    return -1;
    return app.exec();
    The client:
    #include <QCoreApplication>
    #include <QLocalSocket>
    #include "qjsonrpcservice.h"
    int main(int argc, char **argv) {
    QCoreApplication app(argc, argv);
    QLocalSocket socket;
    if (!socket.waitForConnected()) {
    qDebug() << "couldn't connect to local server: " << socket.errorString();
    return -1;
    QEventLoop loop;
    QJsonRpcServiceSocket service(&socket);
    QJsonRpcServiceReply *reply = service.invokeRemoteMethod("service.currentTime");
    QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
    qDebug() << "response: " << reply->response();
    That's all :)

  • Hi belab,

    is the class QJsonRpcService inbuilt in QT or we have to write it on our own ?

  • Hi RohitIti,
    unfortunately it's not part of QT, but it's available from the link above and easy to compile and integrate. So there's no need to write it on your own.

Log in to reply