QtBase  v6.3.1
Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
QHostInfo Class Reference

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
 
QHostInfooperator= (const QHostInfo &d)
 
QHostInfooperator= (QHostInfo &&other) noexcept
 
 ~QHostInfo ()
 
void swap (QHostInfo &other) noexcept
 
QString hostName () const
 
void setHostName (const QString &name)
 
QList< QHostAddressaddresses () 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)
 

Detailed Description

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.

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.
Since Qt 4.6.3 QHostInfo is using a small internal 60 second DNS cache for performance improvements.
See also
QAbstractSocket, {RFC 3492}, {RFC 6724}

Definition at line 54 of file qhostinfo.h.

Member Enumeration Documentation

◆ HostInfoError

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.

See also
error(), setError()
Enumerator
NoError 
HostNotFound 
UnknownError 

Definition at line 57 of file qhostinfo.h.

Constructor & Destructor Documentation

◆ QHostInfo() [1/3]

QHostInfo::QHostInfo ( int  id = -1)
explicit

Constructs an empty host info object with lookup ID id.

See also
lookupId()

Definition at line 567 of file qhostinfo.cpp.

◆ QHostInfo() [2/3]

QHostInfo::QHostInfo ( const QHostInfo other)

Constructs a copy of other.

Definition at line 577 of file qhostinfo.cpp.

◆ QHostInfo() [3/3]

QHostInfo::QHostInfo ( QHostInfo &&  other)
inlinenoexcept

Move-constructs a new QHostInfo from other.

Note
The moved-from object other is placed in a partially-formed state, in which the only valid operations are destruction and assignment of a new value.
Since
5.14

Definition at line 65 of file qhostinfo.h.

◆ ~QHostInfo()

QHostInfo::~QHostInfo ( )

Destroys the host info object.

Definition at line 611 of file qhostinfo.cpp.

Member Function Documentation

◆ abortHostLookup()

void QHostInfo::abortHostLookup ( int  id)
static

Aborts the host lookup with the ID id, as returned by lookupHost().

See also
lookupHost(), lookupId()

Definition at line 357 of file qhostinfo.cpp.

Here is the caller graph for this function:

◆ addresses()

QList< QHostAddress > QHostInfo::addresses ( ) const

Returns the list of IP addresses associated with hostName(). This list may be empty.

Example:

See also
hostName(), error()

Definition at line 626 of file qhostinfo.cpp.

Here is the caller graph for this function:

◆ error()

QHostInfo::HostInfoError QHostInfo::error ( ) const

Returns the type of error that occurred if the host name lookup failed; otherwise returns NoError.

See also
setError(), errorString()

Definition at line 671 of file qhostinfo.cpp.

Here is the caller graph for this function:

◆ errorString()

QString QHostInfo::errorString ( ) const

If the lookup failed, this function returns a human readable description of the error; otherwise "Unknown error" is returned.

See also
setErrorString(), error()

Definition at line 716 of file qhostinfo.cpp.

◆ fromName()

QHostInfo QHostInfo::fromName ( const QString name)
static

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.

See also
lookupHost()

Definition at line 375 of file qhostinfo.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hostName()

QString QHostInfo::hostName ( ) const

Returns the name of the host whose IP addresses were looked up.

See also
localHostName()

Definition at line 648 of file qhostinfo.cpp.

Here is the caller graph for this function:

◆ localDomainName()

QString QHostInfo::localDomainName ( )
static

Returns the DNS domain of this machine.

Note
DNS domains are not related to domain names found in Windows networks.
See also
hostName()

Definition at line 197 of file qhostinfo_unix.cpp.

◆ localHostName()

QString QHostInfo::localHostName ( )
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().

See also
hostName(), localDomainName()

Definition at line 749 of file qhostinfo.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lookupHost() [1/4]

template<typename Func >
static int QHostInfo::lookupHost ( const QString name,
const typename QtPrivate::FunctionPointer< Func >::Object receiver,
Func  slot 
)
inlinestatic

Definition at line 102 of file qhostinfo.h.

◆ lookupHost() [2/4]

template<typename Func >
static std::enable_if<!QtPrivate::FunctionPointer<Func>::IsPointerToMemberFunction && !std::is_same<const char *, Func>::value, int>::type QHostInfo::lookupHost ( const QString name,
Func  slot 
)
inlinestatic

Definition at line 127 of file qhostinfo.h.

◆ lookupHost() [3/4]

template<typename Func1 >
static std::enable_if<!QtPrivate::FunctionPointer<Func1>::IsPointerToMemberFunction && !std::is_same<const char*, Func1>::value, int>::type QHostInfo::lookupHost ( const QString name,
QObject context,
Func1  slot 
)
inlinestatic

Definition at line 136 of file qhostinfo.h.

◆ lookupHost() [4/4]

int QHostInfo::lookupHost ( const QString name,
QObject receiver,
const char *  member 
)
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:

Note
There is no guarantee on the order the signals will be emitted if you start multiple requests with lookupHost().
See also
abortHostLookup(), addresses(), error(), fromName()

Definition at line 270 of file qhostinfo.cpp.

Here is the caller graph for this function:

◆ lookupId()

int QHostInfo::lookupId ( ) const

Returns the ID of this lookup.

See also
setLookupId(), abortHostLookup(), hostName()

Definition at line 693 of file qhostinfo.cpp.

Here is the caller graph for this function:

◆ operator=() [1/2]

QHostInfo & QHostInfo::operator= ( const QHostInfo other)

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.

Here is the call graph for this function:

◆ operator=() [2/2]

QHostInfo & QHostInfo::operator= ( QHostInfo &&  other)
inlinenoexcept

Move-assigns other to this QHostInfo instance.

Note
The moved-from object other is placed in a partially-formed state, in which the only valid operations are destruction and assignment of a new value.
Since
5.10

Definition at line 67 of file qhostinfo.h.

Here is the call graph for this function:

◆ setAddresses()

void QHostInfo::setAddresses ( const QList< QHostAddress > &  addresses)

Sets the list of addresses in this QHostInfo to addresses.

See also
addresses()

Definition at line 637 of file qhostinfo.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setError()

void QHostInfo::setError ( HostInfoError  error)

Sets the error type of this QHostInfo to error.

See also
error(), errorString()

Definition at line 682 of file qhostinfo.cpp.

Here is the call graph for this function:

◆ setErrorString()

void QHostInfo::setErrorString ( const QString str)

Sets the human readable description of the error that occurred to str if the lookup failed.

See also
errorString(), setError()

Definition at line 728 of file qhostinfo.cpp.

◆ setHostName()

void QHostInfo::setHostName ( const QString hostName)

Sets the host name of this QHostInfo to hostName.

See also
hostName()

Definition at line 659 of file qhostinfo.cpp.

Here is the call graph for this function:

◆ setLookupId()

void QHostInfo::setLookupId ( int  id)

Sets the ID of this lookup to id.

See also
lookupId(), lookupHost()

Definition at line 704 of file qhostinfo.cpp.

Here is the caller graph for this function:

◆ swap()

void QHostInfo::swap ( QHostInfo other)
inlinenoexcept

Swaps host-info other with this host-info. This operation is very fast and never fails.

Since
5.10

Definition at line 70 of file qhostinfo.h.

Here is the call graph for this function:

Friends And Related Function Documentation

◆ qt_qhostinfo_lookup

QHostInfo Q_NETWORK_EXPORT qt_qhostinfo_lookup ( const QString name,
QObject receiver,
const char *  member,
bool *  valid,
int *  id 
)
friend

Definition at line 1084 of file qhostinfo.cpp.


The documentation for this class was generated from the following files: