Connect to DBus service via TCP

强颜欢笑 提交于 2019-12-11 03:07:51

问题


I'm new in work with interprocess communication. I need you help and clear explanation. I have 2 applications. One of them is a service and one is a client. I've used QT/C++ to write them. When they are working on the one local PC - all is good. But I need to separate them.

So, I have a service on the PC with IP 192.30.82.101. I connect it to bus using next code:

QDBusConnection connection = QDBusConnection::sessionBus();
connection.registerObject("/my/service/MyService", mySvc);
connection.registerService("my.service.MyService");

Also I have a client on the another PC with IP 192.30.82.40. I connect my client using the next code:

QDBusServiceWatcher serviceWatcher = new QDBusServiceWatcher(); 
serviceWatcher->setConnection(QDBusConnection::sessionBus());
serviceWatcher->addWatchedService("my.service.MyService");     

.....

    myServiceProxy = new local::MyService("my.service.MyService", 
                   "/my/service/MyService", QDBusConnection::sessionBus(), this);

I need to connect my client on the PC with IP 192.30.82.40 to the service on the PC 192.30.82.101. I don't know how to do this. I've found a few topics on the forum but I still don't understand what to do.

So, what I have found:

  1. On the PC where is my service I have to add additional lines to /etc/dbus-1/session.conf:

<listen>tcp:host=<host>,port=<port></listen>

<listen>unix:tmpdir=/tmp</listen>

<auth>ANONYMOUS</auth>

<allow_anonymous/>

Here I have a question: Which IP address I have to use here (server or client)? Which port is used for DBUS (is it default port or how I can check it)? I have tried to set an IP address of my client PC, my service PC and different ports, and port 0, but I've got an error: Failed to bind socket "172.30.82.40:0": Cannot assign requested address.

  1. On the client side I have to set DBUS_SESSION_BUS_ADDRESS with the same address: export DBUS_SESSION_BUS_ADDRESS=tcp:host=<host>,port=<port>.

I've tried to do this with different ports, with 0 port, because this means - use any free port. But I cannot start bus daemon with any port+IP configuration.

Also I've tried to connect client QDBusServiceWatcher to bus using the next code:

serviceWatcher->setConnection(QDBusConnection::connectToBus("tcp:host=<host>,port=<port>", "session"));

It was not successful. I have no idea how to connect them to each other. Can anybody, please, explain me how it should be, how does it work? I want to clearly understanding how does it work?

I will be very grateful for any help. I hope for your kindness.

UPDATE I have found how to connect my client to DBus bus via TCP:

  1. On the PC where is my service I have to add additional lines to /etc/dbus-1/session.conf:

<listen>tcp:host=localhost,bind=*,port=6667,family=ipv4</listen>

<listen>unix:tmpdir=/tmp</listen>

<auth>ANONYMOUS</auth>

<allow_anonymous/>

Here we should add PORT. We can find it in dbus config file.

  1. On the client side I have to set DBUS_SESSION_BUS_ADDRESS with corresponding address (IP of PC where is service): export DBUS_SESSION_BUS_ADDRESS=tcp:host=192.50.88.10,port=6667,family=ipv4.

Thats all. We can check it just started dbus-monitor.

But now I have another issue: how to connect my client to my service? I need to do something more to connect to my service. I guess that this is something like:

QDBusConnection::connectToBus("tcp:host=<host>,port=<port>","connectionName");

I've tried to connect with any random name, but this is not correct. So, my question is - where I can get correct connection name?


回答1:


Correct answer to my question is:

  1. On the PC where is one application add additional lines to /etc/dbus-1/session.conf: <listen>tcp:host=localhost,bind=*,port=6667,family=ipv4</listen>

    <listen>unix:tmpdir=/tmp</listen>

    <auth>ANONYMOUS</auth>

    <allow_anonymous/>

    Correct port you can find in dbus config file.

  2. On the client side It's needed to set DBUS_SESSION_BUS_ADDRESS with corresponding address (IP of PC where is service):
    export DBUS_SESSION_BUS_ADDRESS=tcp:host=192.50.88.10,port=6667,family=ipv4.

  3. To connect to session where is alive your remote app use the next connection string:

    DBusConnection::connectToBus("tcp:host=192.50.88.10,port=6667", "qt_default_session_bus")); 
    

    How to know session name? I've added log in my service:

    qDebug() << "Connection name: " << connection.name(); 
    

    Then started app, copy printed name and set it in my client.

Now it works!



来源:https://stackoverflow.com/questions/45484655/connect-to-dbus-service-via-tcp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!