QtBase
v6.3.1
|
The QHostInfo class provides static functions for host name lookups. More...
#include <qhostinfo.h>
Public Types | |
enum | HostInfoError { NoError , HostNotFound , UnknownError } |
Public Member Functions | |
QHostInfo (int lookupId=-1) | |
QHostInfo (const QHostInfo &d) | |
QHostInfo (QHostInfo &&other) noexcept | |
QHostInfo & | operator= (const QHostInfo &d) |
QHostInfo & | operator= (QHostInfo &&other) noexcept |
~QHostInfo () | |
void | swap (QHostInfo &other) noexcept |
QString | hostName () const |
void | setHostName (const QString &name) |
QList< QHostAddress > | addresses () const |
void | setAddresses (const QList< QHostAddress > &addresses) |
HostInfoError | error () const |
void | setError (HostInfoError error) |
QString | errorString () const |
void | setErrorString (const QString &errorString) |
void | setLookupId (int id) |
int | lookupId () const |
Static Public Member Functions | |
static int | lookupHost (const QString &name, QObject *receiver, const char *member) |
static void | abortHostLookup (int lookupId) |
static QHostInfo | fromName (const QString &name) |
static QString | localHostName () |
static QString | localDomainName () |
template<typename Func > | |
static int | lookupHost (const QString &name, const typename QtPrivate::FunctionPointer< Func >::Object *receiver, Func slot) |
template<typename Func > | |
static std::enable_if<!QtPrivate::FunctionPointer< Func >::IsPointerToMemberFunction &&!std::is_same< const char *, Func >::value, int >::type | lookupHost (const QString &name, Func slot) |
template<typename Func1 > | |
static std::enable_if<!QtPrivate::FunctionPointer< Func1 >::IsPointerToMemberFunction &&!std::is_same< const char *, Func1 >::value, int >::type | lookupHost (const QString &name, QObject *context, Func1 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 54 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 57 of file qhostinfo.h.
|
explicit |
Constructs an empty host info object with lookup ID id.
Definition at line 567 of file qhostinfo.cpp.
Constructs a copy of other.
Definition at line 577 of file qhostinfo.cpp.
|
inlinenoexcept |
Move-constructs a new QHostInfo from other.
Definition at line 65 of file qhostinfo.h.
QHostInfo::~QHostInfo | ( | ) |
Destroys the host info object.
Definition at line 611 of file qhostinfo.cpp.
|
static |
Aborts the host lookup with the ID id, as returned by lookupHost().
Definition at line 357 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 626 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 671 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 716 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 375 of file qhostinfo.cpp.
QString QHostInfo::hostName | ( | ) | const |
Returns the name of the host whose IP addresses were looked up.
Definition at line 648 of file qhostinfo.cpp.
|
static |
Returns the DNS domain of this machine.
Definition at line 197 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 749 of file qhostinfo.cpp.
|
inlinestatic |
Definition at line 102 of file qhostinfo.h.
|
inlinestatic |
Definition at line 127 of file qhostinfo.h.
|
inlinestatic |
Definition at line 136 of file qhostinfo.h.
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:
Definition at line 270 of file qhostinfo.cpp.
int QHostInfo::lookupId | ( | ) | const |
Returns the ID of this lookup.
Definition at line 693 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 598 of file qhostinfo.cpp.
Move-assigns other to this QHostInfo instance.
Definition at line 67 of file qhostinfo.h.
void QHostInfo::setAddresses | ( | const QList< QHostAddress > & | addresses | ) |
Sets the list of addresses in this QHostInfo to addresses.
Definition at line 637 of file qhostinfo.cpp.
void QHostInfo::setError | ( | HostInfoError | error | ) |
Sets the error type of this QHostInfo to error.
Definition at line 682 of file qhostinfo.cpp.
Sets the human readable description of the error that occurred to str if the lookup failed.
Definition at line 728 of file qhostinfo.cpp.
Sets the host name of this QHostInfo to hostName.
Definition at line 659 of file qhostinfo.cpp.
void QHostInfo::setLookupId | ( | int | id | ) |
Sets the ID of this lookup to id.
Definition at line 704 of file qhostinfo.cpp.
Swaps host-info other with this host-info. This operation is very fast and never fails.
Definition at line 70 of file qhostinfo.h.
|
friend |
Definition at line 1084 of file qhostinfo.cpp.