所有IP地址
foreach (const QHostAddress &address, QNetworkInterface::allAddresses()) {
if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost))
qDebug() << address.toString();
}
或ARP掃描
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
for(int i = 0; i < ipAddressesList.size(); ++ i) {
if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
ipAddressesList.at(i).toIPv4Address()) {
ipAddress = ipAddressesList.at(i).toString();
if(ipAddress.left(3) == "192") {
break;
}
}
}
ipAddress = ipAddress.left(ipAddress.lastIndexOf(".") + 1);
for(int i = 0; i <= 255; ++i) {
m_socketsPool.append(new QTcpSocket(this));
QString currentHost = ipAddress + QString::number(i);
connect(m_socketsPool.at(i), SIGNAL(readyRead()), this, SLOT(readFortune()));
connect(m_socketsPool.at(i), SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
m_socketsPool.at(i)->connectToHost(currentHost, yourHost);
}
void readFortune()
{
int i = 0;
for(; i < m_socketsPool.size(); ++i) {
if(sender() == m_socketsPool.at(i)) {
QString currentHost = ipAddress + QString::number(i);
qDebug()<<currentHost;
ipStringList +=(QStringList() << currentHost);
break;
}
}
emit onOutStr(ipStringList);
}
你是什麼意思的「積極」?我猜想要確定他們的網絡設備是否處於活動狀態,您需要發送ping或其他形式的網絡數據包並分析答案。 – xander
如何通過Qt發送ping或包? – matteo
也許看看這個問題的答案:https://stackoverflow.com/questions/22935103/get-the-ping-from-a-remote-target-with-qt-windows-linux – xander