Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
|
The QHostInfo class provides static functions for host name lookups. More...
#include <qhostinfo.h>
Public Types | |
enum | HostInfoError { NoError , HostNotFound , UnknownError } |
This enum describes the various errors that can occur when trying to resolve a host name. More... | |
Public Member Functions | |
QHostInfo (int lookupId=-1) | |
Constructs an empty host info object with lookup ID id. | |
QHostInfo (const QHostInfo &d) | |
Constructs a copy of other. | |
QHostInfo (QHostInfo &&other) noexcept | |
Move-constructs a new QHostInfo from other. | |
QHostInfo & | operator= (const QHostInfo &d) |
Assigns the data of the other object to this host info object, and returns a reference to it. | |
QHostInfo & | operator= (QHostInfo &&other) noexcept |
Move-assigns other to this QHostInfo instance. | |
~QHostInfo () | |
Destroys the host info object. | |
void | swap (QHostInfo &other) noexcept |
\memberswap{host-info} | |
QString | hostName () const |
Returns the name of the host whose IP addresses were looked up. | |
void | setHostName (const QString &name) |
Sets the host name of this QHostInfo to hostName. | |
QList< QHostAddress > | addresses () const |
Returns the list of IP addresses associated with hostName(). | |
void | setAddresses (const QList< QHostAddress > &addresses) |
Sets the list of addresses in this QHostInfo to addresses. | |
HostInfoError | error () const |
Returns the type of error that occurred if the host name lookup failed; otherwise returns NoError. | |
void | setError (HostInfoError error) |
Sets the error type of this QHostInfo to error. | |
QString | errorString () const |
If the lookup failed, this function returns a human readable description of the error; otherwise "Unknown error" is returned. | |
void | setErrorString (const QString &errorString) |
Sets the human readable description of the error that occurred to str if the lookup failed. | |
void | setLookupId (int id) |
Sets the ID of this lookup to id. | |
int | lookupId () const |
Returns the ID of this lookup. | |
Static Public Member Functions | |
static int | lookupHost (const QString &name, const QObject *receiver, const char *member) |
Looks up the IP address(es) associated with host name name, and returns an ID for the lookup. | |
static void | abortHostLookup (int lookupId) |
Aborts the host lookup with the ID id, as returned by lookupHost(). | |
static QHostInfo | fromName (const QString &name) |
Looks up the IP address(es) for the given host name. | |
static QString | localHostName () |
Returns this machine's host name, if one is configured. | |
static QString | localDomainName () |
Returns the DNS domain of this machine. | |
template<typename Functor > | |
static int | lookupHost (const QString &name, const typename QtPrivate::ContextTypeForFunctor< Functor >::ContextType *receiver, Functor &&func) |
template<typename Functor > | |
static int | lookupHost (const QString &name, Functor &&slot) |
Friends | |
QHostInfo Q_NETWORK_EXPORT | qt_qhostinfo_lookup (const QString &name, QObject *receiver, const char *member, bool *valid, int *id) |
The QHostInfo class provides static functions for host name lookups.
\reentrant \inmodule QtNetwork
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:
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 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 \l {RFC 6724} there is no guarantee that all IP addresses registered for a domain or host will be returned.
Definition at line 18 of file qhostinfo.h.
This enum describes the various errors that can occur when trying to resolve a host name.
\value NoError The lookup was successful. \value HostNotFound No IP addresses were found for the host. \value UnknownError An unknown error occurred.
Enumerator | |
---|---|
NoError | |
HostNotFound | |
UnknownError |
Definition at line 22 of file qhostinfo.h.
|
explicit |
Constructs an empty host info object with lookup ID id.
Definition at line 527 of file qhostinfo.cpp.
QHostInfo::QHostInfo | ( | const QHostInfo & | d | ) |
Constructs a copy of other.
Definition at line 537 of file qhostinfo.cpp.
|
inlinenoexcept |
Move-constructs a new QHostInfo from other.
Definition at line 30 of file qhostinfo.h.
QHostInfo::~QHostInfo | ( | ) |
Destroys the host info object.
Definition at line 571 of file qhostinfo.cpp.
|
static |
Aborts the host lookup with the ID id, as returned by lookupHost().
Definition at line 317 of file qhostinfo.cpp.
QList< QHostAddress > QHostInfo::addresses | ( | ) | const |
Returns the list of IP addresses associated with hostName().
This list may be empty.
Example:
Definition at line 586 of file qhostinfo.cpp.
QHostInfo::HostInfoError QHostInfo::error | ( | ) | const |
Returns the type of error that occurred if the host name lookup failed; otherwise returns NoError.
Definition at line 631 of file qhostinfo.cpp.
QString QHostInfo::errorString | ( | ) | const |
If the lookup failed, this function returns a human readable description of the error; otherwise "Unknown error" is returned.
Definition at line 676 of file qhostinfo.cpp.
Looks up the IP address(es) for the given host name.
The function blocks during the lookup which means that execution of the program is suspended until the results of the lookup are ready. Returns the result of the lookup in a QHostInfo object.
If you pass a literal IP address to name instead of a host name, QHostInfo will search for the domain name for the IP (i.e., QHostInfo will perform a reverse lookup). On success, the returned QHostInfo will contain both the resolved domain name and IP addresses for the host name.
Definition at line 335 of file qhostinfo.cpp.
QString QHostInfo::hostName | ( | ) | const |
Returns the name of the host whose IP addresses were looked up.
Definition at line 608 of file qhostinfo.cpp.
|
static |
Returns the DNS domain of this machine.
Definition at line 96 of file qhostinfo_unix.cpp.
|
static |
Returns this machine's host name, if one is configured.
Note that hostnames are not guaranteed to be globally unique, especially if they were configured automatically.
This function does not guarantee the returned host name is a Fully Qualified Domain Name (FQDN). For that, use fromName() to resolve the returned name to an FQDN.
This function returns the same as QSysInfo::machineHostName().
Definition at line 709 of file qhostinfo.cpp.
|
static |
Looks up the IP address(es) associated with host name name, and returns an ID for the lookup.
When the result of the lookup is ready, the slot or signal member in receiver is called with a QHostInfo argument. The QHostInfo object can then be inspected to get the results of the lookup.
The lookup is performed by a single function call, for example:
The implementation of the slot prints basic information about the addresses returned by the lookup, or reports an error if it failed:
If you pass a literal IP address to name instead of a host name, QHostInfo will search for the domain name for the IP (i.e., QHostInfo will perform a reverse lookup). On success, the resulting QHostInfo will contain both the resolved domain name and IP addresses for the host name. Example:
{QObject*}.Definition at line 229 of file qhostinfo.cpp.
|
inlinestatic |
Definition at line 68 of file qhostinfo.h.
|
inlinestatic |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Looks up the IP address(es) associated with host name name, and returns an ID for the lookup. When the result of the lookup is ready, the functor is called with a QHostInfo argument. The QHostInfo object can then be inspected to get the results of the lookup.
The functor will be run in the thread that makes the call to lookupHost; that thread must have a running Qt event loop.
Definition at line 83 of file qhostinfo.h.
int QHostInfo::lookupId | ( | ) | const |
Returns the ID of this lookup.
Definition at line 653 of file qhostinfo.cpp.
Assigns the data of the other object to this host info object, and returns a reference to it.
Definition at line 558 of file qhostinfo.cpp.
Move-assigns other to this QHostInfo instance.
Definition at line 32 of file qhostinfo.h.
void QHostInfo::setAddresses | ( | const QList< QHostAddress > & | addresses | ) |
Sets the list of addresses in this QHostInfo to addresses.
Definition at line 597 of file qhostinfo.cpp.
void QHostInfo::setError | ( | HostInfoError | error | ) |
Sets the error type of this QHostInfo to error.
Definition at line 642 of file qhostinfo.cpp.
Sets the human readable description of the error that occurred to str if the lookup failed.
Definition at line 688 of file qhostinfo.cpp.
Sets the host name of this QHostInfo to hostName.
Definition at line 619 of file qhostinfo.cpp.
void QHostInfo::setLookupId | ( | int | id | ) |
Sets the ID of this lookup to id.
Definition at line 664 of file qhostinfo.cpp.
|
friend |
Definition at line 1057 of file qhostinfo.cpp.