QHostInfo Class Reference

[ QtNetwork module]

The QHostInfo class provides static functions for host name lookups. 更多...

类型

方法

Static Methods


详细描述

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.

要异步查找主机的 IP 地址,调用 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 () 采用 the lookup ID.

范例:

 // To find the IP address of qt.nokia.com
 QHostInfo.lookupHost("qt.nokia.com",
                       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.

若希望阻塞查找,使用 QHostInfo.fromName () 函数:

 QHostInfo info = QHostInfo.fromName("qt.nokia.com");
			

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 () 函数。

注意: 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 使用 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.


类型文档编制

QHostInfo.HostInfoError

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

常量 描述
QHostInfo.NoError 0 查找成功。
QHostInfo.HostNotFound 1 No IP addresses were found for the host.
QHostInfo.UnknownError 2 出现未知错误。

另请参阅 error () 和 setError ().


方法文档编制

QHostInfo.__init__ ( self , int  id  = -1)

Constructs an empty host info object with lookup ID id .

另请参阅 lookupId ().

QHostInfo.__init__ ( self , QHostInfo   d )

构造副本为 other .

QHostInfo.abortHostLookup (int  lookupId )

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

另请参阅 lookupHost () 和 lookupId ().

list-of-QHostAddress QHostInfo.addresses ( self )

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

范例:

 QHostInfo info;
 ...
 if (!info.addresses().isEmpty()) {
     QHostAddress address = info.addresses().first();
     // use the first IP address
 }
			

另请参阅 setAddresses (), hostName (),和 error ().

HostInfoError QHostInfo.error ( self )

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

另请参阅 setError () and errorString ().

QString QHostInfo.errorString ( self )

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

另请参阅 setErrorString () 和 error ().

QHostInfo QHostInfo.fromName (QString  name )

Looks up the IP address(es) for the given host name 。 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 对象。

If you pass a literal IP address to name 而不是 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.

另请参阅 lookupHost ().

QString QHostInfo.hostName ( self )

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

另请参阅 setHostName () 和 localHostName ().

QString QHostInfo.localDomainName ()

Returns the DNS domain of this machine.

Note: DNS domains are not related to domain names found in Windows networks.

另请参阅 hostName ().

QString QHostInfo.localHostName ()

Returns the host name of this machine.

另请参阅 hostName ().

int QHostInfo.lookupHost (QString  name , QObject   receiver , SLOT(QHostInfo)SLOT()  member )

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 自变量。 QHostInfo object can then be inspected to get the results of the lookup.

The lookup is performed by a single function call, for example:

 QHostInfo.lookupHost("www.kde.org",
                       this, SLOT(lookedUp(QHostInfo)));
			

The implementation of the slot prints basic information about the addresses returned by the lookup, or reports an error if it failed:

 void MyWidget.lookedUp(const QHostInfo &host)
 {
     if (host.error() != QHostInfo.NoError) {
         qDebug() << "Lookup failed:" << host.errorString();
         return;
     }
     foreach (const QHostAddress &address, host.addresses())
         qDebug() << "Found address:" << address.toString();
 }
			

If you pass a literal IP address to name 而不是 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:

 QHostInfo.lookupHost("4.2.2.1",
                       this, SLOT(lookedUp(QHostInfo)));
			

注意: There is no guarantee on the order the signals will be emitted if you start multiple requests with lookupHost().

另请参阅 abortHostLookup (), addresses (), error (),和 fromName ().

int QHostInfo.lookupHost (QString  name , callable  receiver )

int QHostInfo.lookupId ( self )

Returns the ID of this lookup.

另请参阅 setLookupId (), abortHostLookup (),和 hostName ().

QHostInfo.setAddresses ( self , list-of-QHostAddress  addresses )

Sets the list of addresses in this QHostInfo to addresses .

另请参阅 addresses ().

QHostInfo.setError ( self , HostInfoError   error )

Sets the error type of this QHostInfo to error .

另请参阅 error () 和 errorString ().

QHostInfo.setErrorString ( self , QString  errorString )

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

另请参阅 errorString () 和 setError ().

QHostInfo.setHostName ( self , QString  name )

Sets the host name of this QHostInfo to hostName .

另请参阅 hostName ().

QHostInfo.setLookupId ( self , int  id )

Sets the ID of this lookup to id .

另请参阅 lookupId () and lookupHost ().