通过TCP连接到DBus服务 [英] Connect to DBus service via TCP
问题描述
我是进行进程间通信的新手。我需要您的帮助和明确的解释。我有2个应用程序。其中之一是服务,一个是客户。我使用QT / C ++编写它们。当他们在一台本地PC上工作时-一切都很好。但是我需要将它们分开。
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.
因此,我在IP为192.30.82.101的PC上有一项服务。
我使用下一个代码将其连接到总线:
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");
我在另一台IP为192.30.82.40的PC上也有一个客户端。
我使用以下代码连接客户端:
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);
我需要将IP 192.30.82.40的PC上的客户端连接到192.30 PC上的服务.82.101。我不知道该怎么做。我在论坛上找到了一些主题,但我仍然不知道该怎么办。
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.
所以,我发现的东西:
- 在PC上我的位置在哪里服务我必须在/etc/dbus-1/session.conf中添加其他行:
< listen> tcp:host =< host>,port =< port>< / listen>
< listen& unix:tmpdir = / tmp< / listen>
< auth> ANONYMOUS< / auth> ;
< allow_anonymous />
这里我有一个问题:我必须在此处使用哪个IP地址(服务器或客户端)? DBUS使用哪个端口(是默认端口还是如何检查它)?我试图设置客户端PC,服务PC和其他端口以及端口0的IP地址,但出现错误:无法绑定套接字 172.30.82.40:0:无法分配请求的地址。 / p>
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.
- 在客户端,我必须使用相同的地址设置DBUS_SESSION_BUS_ADDRESS:
export DBUS_SESSION_BUS_ADDRESS = tcp :host =< host>,port =< port>
。
- 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>
.
我试图用不同的端口(端口为0)执行此操作,因为这意味着-使用任何可用端口。但是我无法使用任何端口+ IP配置启动总线守护程序。
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.
此外,我还尝试使用以下代码将客户端QDBusServiceWatcher连接到总线:
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.
更新
我发现了如何将客户端连接到DBus通过TCP的总线:
UPDATE I have found how to connect my client to DBus bus via TCP:
- 在PC上,我的服务是在/ 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 />
应该添加PORT。我们可以在dbus配置文件中找到它。
Here we should add PORT. We can find it in dbus config file.
- 在客户端,我必须将DBUS_SESSION_BUS_ADDRESS设置为相应的地址(IP的PC所在的服务):
导出DBUS_SESSION_BUS_ADDRESS = tcp:host = 192.50.88.10,port = 6667,family = ipv4
。
- 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
.
仅此而已。我们可以检查它是否刚刚启动了dbus-monitor。
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?
推荐答案
我的问题的正确答案是:
-
在PC上,一个应用程序在其中添加了其他行
/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 />
可以在dbus配置文件中找到正确的端口。
Correct port you can find in dbus config file.
需要在DBUS_SESSION_BUS_ADDRESS上设置
的对应地址(服务所在的PC的IP):
导出DBUS_SESSION_BUS_ADDRESS = tcp:host = 192.50.88.10,port = 6667,family = ipv4
。
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
.
要在以下位置连接到会话还活着,您的远程应用程序使用下一个
con否定字符串:
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.
现在可以使用了!
这篇关于通过TCP连接到DBus服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!