QZeroConf is a Qt wrapper class for ZeroConf libraries across various platforms.
- Windows (requires iTunes or Apple Print Services)
- Mac
- Linux
- Android
- iOS
QZeroConf wraps avahi-client on Linux, avahi-core on Android, and dnssd on Mac, iOS and Windows.
QZeroConf can be built directly into your project if your project is LGPL3 compatible. If your project is closed source, you can build QZeroConf as a dynamic library and link against it.
-
Clone or download QZeroConf. If you download, unzip.
-
Copy the qtzeroconf directory to be under your project's directory.
-
Include the qtzeroconf.pri file in your projects .pro file
include(qtzeroconf/qtzeroconf.pri)
-
Add QZEROCONF_STATIC define in your projects .pro file
DEFINES= QZEROCONF_STATIC
- Clone or download QZeroConf. If you download, unzip.
- Enter the qtzeroconf directory, run qmake and then make.
(See the example included with the source)
- Include header
#include "qzeroconf.h"
- Create an instance of QZeroConf
QZeroConf zeroConf;
It is recommend, but not required, that you connect a slot to QZeroConf's error() signal and servicePublished() signal.
- If you want to add one or more txt records to the service, call
zeroConf.addServiceTxtRecord("name", "value");
or
zeroConf.addServiceTxtRecord("nameOnly");
before calling startServicePublish()
- Call startServicePublish() with the name, type, domain and port of your service.
zeroConf.startServicePublish("Test", "_test._tcp", "local", 12345);
QZeroConf will emit servicePublished() if successful, or the error() signal if registration fails.
Service publishing can be stopped by calling stopServicePublish(). Only one service can be published per instance of QZeroConf.
(See the example included with the source)
- Include header
#include "qzeroconf.h"
- Create an instance of QZeroConf
QZeroConf zeroConf;
It is recommend, but not required, that you connect a slot to QZeroConf's error() signal.
-
Connect a slot to QZeroConf's serviceAdded() signal. When serviceAdded() is emitted, it passes a pointer to the QZeroConfService recently discovered.
-
Optionally connect a slot to QZeroConf's serviceRemoved() signal to received status when the service is unpublished. ServiceRemoved() passes a pointer to the service being removed. Do not make delayed connections with ServiceRemoved() signal.
-
Call startBrowser() with the type of the service to browse for and optionally the protocol to use.
startBrowser("_test._tcp");
If you are browsing for services published using both ipv4 and ipv6 ( QAbstractSocket::AnyIPProtocol) you should also connect a slot to QzeroConf's serviceUpdated() signal. When the IP address of the first protocol is resolved, serviceAdded() is emitted, when the IP address of the second protocol is resolved, serviceUpdated() is emitted.
Only one browser can be in use per instance of QzeroConf.
Txt records are placed into a QMap called txt within the discovered service. For example, the value of txt record "Qt=The Best!" can be retrieved with the code...
qDebug() << zcs->txt["Qt"];
Qt5
On Linux, libavahi-client-dev and libavahi-common-dev
Publishing GPL software in the App Store is a violation of the GPL. If you need to publish an app in the Apple App Store that uses QZeroConf, please contact me for a copy of QZeroConf with a BSD licence.
When iOS puts the device to sleep, it breaks the DNS-SD browser and service publisher. The only way around this is to call stopServicePublish() and stopBrowser() when the application state changes to Qt::ApplicationSuspended (sleep) and then call startPublish() and startBrowser() when the application state changes to Qt::ApplicationActive (wake). See appStateChanged() in example.