QHostInfo Class

The QHostInfo class provides static functions for host name lookups. More...

Header: #include <QHostInfo>
qmake: QT += network

Note: All functions in this class are reentrant.

Public Types

enum HostInfoError { NoError, HostNotFound, UnknownError }

Detailed Description

QHostInfo finds the IP address(es) associated with a host name, or the host name associated with an IP address. The class provides two static convenience functions: one that works asynchronously and emits a signal once the host is found, and one that blocks and returns a QHostInfo object.

To look up a host's IP addresses asynchronously, call lookupHost(), which takes the host name or IP address, a receiver object, and a slot signature as arguments and returns an ID. You can abort the lookup by calling abortHostLookup() with the lookup ID.

Example:

 // To find the IP address of qt-project.org
 QHostInfo::lookupHost("qt-project.org",
                       this, SLOT(printResults(QHostInfo)));

 // To find the host name for 4.2.2.1
 QHostInfo::lookupHost("4.2.2.1",
                       this, SLOT(printResults(QHostInfo)));

The slot is invoked when the results are ready. The results are stored in a QHostInfo object. Call addresses() to get the list of IP addresses for the host, and hostName() to get the host name that was looked up.

If the lookup failed, error() returns the type of error that occurred. errorString() gives a human-readable description of the lookup error.

If you want a blocking lookup, use the QHostInfo::fromName() function:

 QHostInfo info = QHostInfo::fromName("qt-project.org");

QHostInfo supports Internationalized Domain Names (IDNs) through the IDNA and Punycode standards.

To retrieve the name of the local host, use the static QHostInfo::localHostName() function.

QHostInfo uses the mechanisms provided by the operating system to perform the lookup. As per {https://tools.ietf.org/html/rfc6724}{RFC 6724} there is no guarantee that all IP addresses registered for a domain or host will be returned.

Note: Since Qt 4.6.1 QHostInfo is using multiple threads for DNS lookup instead of one dedicated DNS thread. This improves performance, but also changes the order of signal emissions when using lookupHost() compared to previous versions of Qt.

Note: Since Qt 4.6.3 QHostInfo is using a small internal 60 second DNS cache for performance improvements.

See also QAbstractSocket, RFC 3492, and RFC 6724.

Member Type Documentation

enum QHostInfo::HostInfoError

This enum describes the various errors that can occur when trying to resolve a host name.

ConstantValueDescription
QHostInfo::NoError0The lookup was successful.
QHostInfo::HostNotFound1No IP addresses were found for the host.
QHostInfo::UnknownError2An unknown error occurred.

See also error() and setError().