QHostInfo Class Reference
[QtNetwork module]
该QHostInfo类提供静态函数为主机名查找。More…
Types
enum HostInfoError { NoError, HostNotFound, UnknownError }
Methods
__init__ (self, int id = -1)
__init__ (self, QHostInfo d)
list-of-QHostAddress addresses (self)
HostInfoError error (self)
QString errorString (self)
QString hostName (self)
int lookupId (self)
setAddresses (self, list-of-QHostAddress addresses)
setError (self, HostInfoError error)
setErrorString (self, QString errorString)
setHostName (self, QString name)
setLookupId (self, int id)
Static Methods
abortHostLookup (int lookupId)
QHostInfo fromName (QString name)
QString localDomainName ()
QString localHostName ()
int lookupHost (QString name, QObject receiver, SLOT(QHostInfo)SLOT() member)
int lookupHost (QString name, callable receiver)
Detailed Description
该QHostInfo类提供静态函数为主机名查找。
QHostInfo使用提供的操作系统找到一个主机名,或与IP地址对应的主机名相关的IP地址( ES )的查找机制。这个类提供了两个静态的便利功能:一个以异步方式工作,并发出信号,一旦主机被发现,而另一个块,并返回一个QHostInfo对象。
要查找主机的IP地址异步调用lookupHost( ) ,这需要主机名或IP地址,接收对象和槽签名作为参数并返回一个ID 。你可以通过调用中止查询abortHostLookup( )与查找的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)));
该槽被调用时,结果准备就绪。结果被存储在一个QHostInfo对象。通话addresses( )来获得主机的IP地址列表,并hostName()来获取被查找的主机名。
如果查找失败,error()返回发生错误的类型。errorString( )给出了查找错误的人类可读的描述。
如果你想有一个阻塞的查找,使用QHostInfo.fromName( )函数:
QHostInfo info = QHostInfo.fromName("qt.nokia.com");
QHostInfo支持国际化域名(IDN )通过IDNA和Punycode的标准。
检索本地主机的名称,使用静态QHostInfo.localHostName()函数。
Note:由于Qt的4.6.1 QHostInfo使用多线程进行DNS查询,而不是一个专用的DNS线程。这提高了性能,而且还当使用改变信号的排放顺序lookupHost( )相比, Qt之前版本。Note:由于Qt的4.6.3 QHostInfo使用以提高性能,一个小的内部60秒DNS缓存。
Type Documentation
QHostInfo.HostInfoError
这个枚举描述试图解析主机名时可能出现的各种错误。
Constant | Value | Description |
---|---|---|
QHostInfo.NoError |
0 |
该查找是成功的。 |
QHostInfo.HostNotFound |
1 |
未找到主机没有IP地址。 |
QHostInfo.UnknownError |
2 |
发生未知错误。 |
See also error()和setError( ) 。
Method Documentation
QHostInfo.__init__ (self, int id = -1)
构造一个空的主机信息对象查找的IDid。
See also lookupId( ) 。
QHostInfo.__init__ (self, QHostInfo d)
构造的副本other。
QHostInfo.abortHostLookup (int lookupId)
中止主机查找的IDid,所返回的lookupHost( ) 。
See also lookupHost()和lookupId( ) 。
list-of-QHostAddress QHostInfo.addresses (self)
返回与关联的IP地址列表hostName( ) 。这个列表可能是空的。
例如:
[QHostInfo]($docs-qhostinfo.html) info;
...
if (!info.addresses().isEmpty()) {
[QHostAddress](qhostaddress.html) address = info.addresses().first();
// use the first IP address
}
See also setAddresses( )hostName()和error( ) 。
HostInfoError QHostInfo.error (self)
返回,如果主机名查找失败时发生错误的类型,否则返回NoError。
See also setError()和errorString( ) 。
QString QHostInfo.errorString (self)
如果查找失败,则此函数返回错误的人类可读的描述;否则返回“未知错误” 。
See also setErrorString()和error( ) 。
QHostInfo QHostInfo.fromName (QString name)
对于给定的主机查找IP地址( ES )name。查找这意味着在程序执行过程中的功能块被挂起,直到查找的结果是准备好了。返回在该查询的结果QHostInfo对象。
如果你传递一个文字IP地址name而不是一个主机名,QHostInfo将搜索的域名为IP (即,QHostInfo将执行reverse查找) 。如果成功,则返回QHostInfo将包含解析域名和IP地址的主机名。
See also lookupHost( ) 。
QString QHostInfo.hostName (self)
返回主机的IP地址进行查找时的名称。
See also setHostName()和localHostName( ) 。
QString QHostInfo.localDomainName ()
返回本机的DNS域。
注: DNS域是不相关的,在Windows网络中存在的域名。
See also hostName( ) 。
QString QHostInfo.localHostName ()
返回本机的主机名。
See also hostName( ) 。
int QHostInfo.lookupHost (QString name, QObject receiver, SLOT(QHostInfo)SLOT() member)
查找与主机名相关的IP地址( ES )name,并返回查找的ID。当查找的结果是准备好了,槽或信号member在receiver被称为一个QHostInfo的说法。该QHostInfo对象可以被检查,以得到该查找的结果。
该查找是一个函数调用,例如执行:
[QHostInfo]($docs-qhostinfo.html).lookupHost("www.kde.org",
this, SLOT(lookedUp([QHostInfo]($docs-qhostinfo.html))));
槽的实施打印有关的查询返回的地址的基本信息,或报告,如果它失败的错误:
void MyWidget.lookedUp(const [QHostInfo]($docs-qhostinfo.html) &host)
{
if (host.error() != [QHostInfo]($docs-qhostinfo.html).NoError) {
qDebug() << "Lookup failed:" << host.errorString();
return;
}
foreach (const [QHostAddress](qhostaddress.html) &address, host.addresses())
qDebug() << "Found address:" << address.toString();
}
如果你传递一个文字IP地址name而不是一个主机名,QHostInfo将搜索的域名为IP (即,QHostInfo将执行reverse查找) 。上的成功,将所得QHostInfo将包含解析域名和IP地址的主机名。例如:
[QHostInfo]($docs-qhostinfo.html).lookupHost("4.2.2.1",
this, SLOT(lookedUp([QHostInfo]($docs-qhostinfo.html))));
Note:有没有保证,如果你开始的多个请求与lookupHost ( )的信号将被发出的顺序。
See also abortHostLookup( )addresses( )error()和fromName( ) 。
int QHostInfo.lookupHost (QString name, callable receiver)
int QHostInfo.lookupId (self)
返回此查询的ID 。
See also setLookupId( )abortHostLookup()和hostName( ) 。
QHostInfo.setAddresses (self, list-of-QHostAddress addresses)
设置在该地址列表QHostInfo至addresses。
See also addresses( ) 。
QHostInfo.setError (self, HostInfoError error)
设置这种错误类型QHostInfo至error。
See also error()和errorString( ) 。
QHostInfo.setErrorString (self, QString errorString)
设置错误的发生,以人类可读的描述str如果查找失败。
See also errorString()和setError( ) 。
QHostInfo.setHostName (self, QString name)
设置该主机名QHostInfo至hostName。
See also hostName( ) 。
QHostInfo.setLookupId (self, int id)
设置此查询的IDid。
See also lookupId()和lookupHost( ) 。