Qt 4.8
|
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 Functions | |
QList< QHostAddress > | addresses () const |
Returns the list of IP addresses associated with hostName(). More... | |
HostInfoError | error () const |
Returns the type of error that occurred if the host name lookup failed; otherwise returns NoError. More... | |
QString | errorString () const |
If the lookup failed, this function returns a human readable description of the error; otherwise "Unknown error" is returned. More... | |
QString | hostName () const |
Returns the name of the host whose IP addresses were looked up. More... | |
int | lookupId () const |
Returns the ID of this lookup. More... | |
QHostInfo & | operator= (const QHostInfo &d) |
Assigns the data of the other object to this host info object, and returns a reference to it. More... | |
QHostInfo (int lookupId=-1) | |
Constructs an empty host info object with lookup ID id. More... | |
QHostInfo (const QHostInfo &d) | |
Constructs a copy of other. More... | |
void | setAddresses (const QList< QHostAddress > &addresses) |
Sets the list of addresses in this QHostInfo to addresses. More... | |
void | setError (HostInfoError error) |
Sets the error type of this QHostInfo to error. More... | |
void | setErrorString (const QString &errorString) |
Sets the human readable description of the error that occurred to str if the lookup failed. More... | |
void | setHostName (const QString &name) |
Sets the host name of this QHostInfo to hostName. More... | |
void | setLookupId (int id) |
Sets the ID of this lookup to id. More... | |
~QHostInfo () | |
Destroys the host info object. More... | |
Static Public Functions | |
static void | abortHostLookup (int lookupId) |
Aborts the host lookup with the ID id, as returned by lookupHost(). More... | |
static QHostInfo | fromName (const QString &name) |
Looks up the IP address(es) for the given host name. More... | |
static QString | localDomainName () |
Returns the DNS domain of this machine. More... | |
static QString | localHostName () |
Returns the host name of this machine. More... | |
static int | lookupHost (const QString &name, QObject *receiver, const char *member) |
Looks up the IP address(es) associated with host name name, and returns an ID for the lookup. More... | |
Properties | |
QScopedPointer< QHostInfoPrivate > | d |
The QHostInfo class provides static functions for host name lookups.
QHostInfo uses the lookup mechanisms provided by the operating system to find 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.
Definition at line 58 of file qhostinfo.h.
This enum describes the various errors that can occur when trying to resolve a host name.
Enumerator | |
---|---|
NoError | |
HostNotFound | |
UnknownError |
Definition at line 61 of file qhostinfo.h.
QHostInfo::QHostInfo | ( | int | id = -1 | ) |
Constructs an empty host info object with lookup ID id.
Definition at line 331 of file qhostinfo.cpp.
QHostInfo::QHostInfo | ( | const QHostInfo & | d | ) |
Constructs a copy of other.
Definition at line 340 of file qhostinfo.cpp.
QHostInfo::~QHostInfo | ( | ) |
|
static |
Aborts the host lookup with the ID id, as returned by lookupHost().
Definition at line 255 of file qhostinfo.cpp.
Referenced by QAbstractSocket::connectToHostImplementation(), QAbstractSocket::disconnectFromHostImplementation(), and QAbstractSocket::waitForConnected().
QList< QHostAddress > QHostInfo::addresses | ( | ) | const |
Returns the list of IP addresses associated with hostName().
This list may be empty.
Example:
Definition at line 372 of file qhostinfo.cpp.
Referenced by QAbstractSocketPrivate::_q_startConnecting(), QHostInfoAgent::fromName(), setAddresses(), and translateWSAError().
QHostInfo::HostInfoError QHostInfo::error | ( | ) | const |
Returns the type of error that occurred if the host name lookup failed; otherwise returns NoError.
Definition at line 413 of file qhostinfo.cpp.
Referenced by QHostInfoAgent::fromName(), QHostInfoCache::put(), setError(), and translateWSAError().
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 454 of file qhostinfo.cpp.
Referenced by QHostInfoAgent::fromName(), and translateWSAError().
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 273 of file qhostinfo.cpp.
Referenced by QSslSocketPrivate::_q_connectedSlot(), and QAbstractSocket::waitForConnected().
QString QHostInfo::hostName | ( | ) | const |
Returns the name of the host whose IP addresses were looked up.
Definition at line 392 of file qhostinfo.cpp.
Referenced by QHostInfoAgent::fromName(), setHostName(), and translateWSAError().
|
static |
Returns the DNS domain of this machine.
Note: DNS domains are not related to domain names found in Windows networks.
Definition at line 333 of file qhostinfo_unix.cpp.
Referenced by interfaceListing().
|
static |
Returns the host name of this machine.
Definition at line 324 of file qhostinfo_unix.cpp.
Referenced by translateWSAError().
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 156 of file qhostinfo.cpp.
Referenced by qt_qhostinfo_lookup().
int QHostInfo::lookupId | ( | ) | const |
Returns the ID of this lookup.
Definition at line 433 of file qhostinfo.cpp.
Referenced by QAbstractSocketPrivate::_q_startConnecting().
Assigns the data of the other object to this host info object, and returns a reference to it.
Definition at line 349 of file qhostinfo.cpp.
void QHostInfo::setAddresses | ( | const QList< QHostAddress > & | addresses | ) |
Sets the list of addresses in this QHostInfo to addresses.
Definition at line 382 of file qhostinfo.cpp.
Referenced by QAbstractSocket::connectToHostImplementation(), QHostInfoAgent::fromName(), and translateWSAError().
void QHostInfo::setError | ( | HostInfoError | error | ) |
Sets the error type of this QHostInfo to error.
Definition at line 423 of file qhostinfo.cpp.
Referenced by QHostInfoAgent::fromName(), lookupHost(), and translateWSAError().
void QHostInfo::setErrorString | ( | const QString & | str | ) |
Sets the human readable description of the error that occurred to str if the lookup failed.
Definition at line 465 of file qhostinfo.cpp.
Referenced by QHostInfoAgent::fromName(), lookupHost(), and translateWSAError().
void QHostInfo::setHostName | ( | const QString & | hostName | ) |
Sets the host name of this QHostInfo to hostName.
Definition at line 402 of file qhostinfo.cpp.
Referenced by QHostInfoAgent::fromName(), and translateWSAError().
void QHostInfo::setLookupId | ( | int | id | ) |
Sets the ID of this lookup to id.
Definition at line 443 of file qhostinfo.cpp.
Referenced by lookupHost(), and QHostInfoRunnable::run().
|
private |
Definition at line 95 of file qhostinfo.h.
Referenced by addresses(), error(), errorString(), hostName(), lookupId(), operator=(), QHostInfo(), setAddresses(), setError(), setErrorString(), setHostName(), and setLookupId().