How to check computer status using Qt?

I am trying to get the status of a computer on my local network ... I thought about using QTcpSocket, but it is not very efficient, since the port should also be inserted as:

socket->connectToHost("hostName", portNumber); if (socket->waitForConnected(1000)) qDebug("Connected!"); 

can someone show me the best way to check if the computer is responding?

+2
source share
3 answers

ping

 int exitCode = QProcess::execute("ping", QStringList() << "-c1" << "hostname"); if (0 == exitCode) { // it alive } else { // it dead } 

Arguments may vary. For example, I believe that on Windows it will be ping -n 1 "hostname" . The example should work on most versions other than Windows.

+1
source

Are you trying to check if your local computer is on the network or any target computer?

There is not a very good cross-platform way to do this. The closest one to qt is QNetworkInterface and check the β€œISup” attribute - it is not perfect, it can be active if you have a network cable connected, but only to the router, and inactive if you have a 3G modem, but not a call.

On Windows, check InternetGetConnectedState ()

+1
source

One good way is to verify that they can resolve domain names using QHostInfo . If they can, then they probably have Internet access:

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

Of course, you could just try connecting to the host, which even better proves that everything is working correctly. I would do it asynchronously, not synchronously, but this is really the best test.

-1
source

Source: https://habr.com/ru/post/1379626/


All Articles