本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.
qt中提供了可以实现域名解析功能的类QHostInfo,这个类解析域名提供两种机制,一种是阻塞式,一种是非阻塞信号槽机制,下面介绍第二种机制的实现方法。
qt的帮助中给出了这个类的例子:
QHostInfo::lookupHost("www.kde.org",this, SLOT(lookedUp(QHostInfo)));
//当解析成功域名后,会调用lookedUp槽函数
void MyWidget::lookedUp(const QHostInfo &host)
{
if (host.error() != QHostInfo::NoError) {
qDebug() << "Lookup failed:" << host.errorString();
return;
}
foreach (QHostAddress address, host.addresses())
qDebug() << "Found address:" << address.toString();
}
仿照这个例子,我做了google域名的解析测试:
QHostInfo::lookupHost("www.google.com",this,SLOT(slot_get_ip(QHostInfo)));
void test::slot_get_ip(QHostInfo host_info)
{
if (host_info.error() != QHostInfo::NoError)
{
qDebug() << "Lookup failed:" << host_info.errorString();
return;
}
for (int i = 0;i < host_info.addresses().size();i++)
{
qDebug() << "Found address:" << host_info.addresses()[i].toString() << endl;
}
}
在调试窗口可以看到解析结果:
Found address: "64.233.183.106"
Found address: "64.233.183.147"
Found address: "64.233.183.99"
Found address: "64.233.183.103"
Found address: "64.233.183.104"
Found address: "64.233.183.105"