tags:

views:

113

answers:

3

i'm trying to get computer's state in my LAN... thought about using QTcpSocket but it's not realy effective since port also should be inserted as:

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

can anyone demonstare me a better way to check if computer is responding ?

+1  A: 

ping

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

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

Judge Maygarden
Some firewalls will block that, and it takes an external system call. That's certainly less than ideal.
Kaleb Pederson
A: 

One good way is just to verify that they can resolve domain names using QHostInfo. If they can then they likely have internet access:

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

Of course, you could just try to connect to the host as well, which is even better proof that everything is working correctly. I would do it asynchronously rather than synchronously, but it's truly the best test.

Kaleb Pederson
The other machine could die and still have an active DNS entry.
Judge Maygarden
Yes, and then it won't matter whether you can ping it or resolve the hostname. In that case, as I said, the best way is to truly try to connect to it.
Kaleb Pederson
A: 

Are you trying to check if your local machine is on the network or if a target machine is ?

There isn't a good cross platform way of doing this. The nearest on qt is QNetworkInterface, and check attribute "ISup" - it's not perfect, it may be active if you have a network cable connected but just to a router, and inactive if you have a 3G modem but aren't on a call.

On Windows check InternetGetConnectedState()

Martin Beckett