[Solved] Ping-like Qt App - QTcpSocket?
-
Greetings!
My company needs a lightweight application that can quickly cycle through a range of IPs on the company network to in order to find out which computers have been disconnected.
I recognize that I can achieve this functionality using QProcess and ping.exe, but QTcpSocket looks like it should be able to provide the desired functionality more simply. Theoretically, I should be able to attempt a connection to a certain IP address, wait a few milliseconds, and move onto the next IP address. For now I'm just trying one IP address, and then I'll wrap it in a loop.
Buut.... when I compile in Qt Creator 2.2.1, Qt 4.7.3, I get an error "request for member 'connectToHost' in 'messenger', which is of non-class type 'QTcpSocket()'." All references to 'messenger' have similar compiler errors. Here's a code snippet:
@#include <QtNetwork/QTcpSocket>
#include <QPlainTextEdit>
void MainWindow::on_pushButton_clicked()
{
QTcpSocket messenger();
messenger.connectToHost("192.168.0.254", 61000);
if(!messenger.waitForConnected(3000))
{
ui->plainTextEdit->appendPlainText(QString("%1").arg(messenger.error()));
}
}@Some questions: Anyone know what's wrong with my usage of QTcpSocket? Is this even the correct Qt class to provide my desired function? Am I using the wrong port to connect to a computer on my network?
Thanks!
-
Remove the () from messenger.
@
TcpSocket messenger; // !messenger()@ -
Oh... duh. Thanks! With that correction, I get a new set of errors:
@error: undefined reference to '_imp___ZN10QTcpSocketC1EP7QObject'
error: undefined reference to_imp___ZN15QAbstractSocket13connectToHostERK7QStringt6QFlagsIN9QIODevice12OpenModeFlagEE' error: undefined reference to
_imp___ZN15QAbstractSocket16waitForConnectedEi'
error: undefined reference to_imp___ZNK15QAbstractSocket5errorEv' error: undefined reference to
_imp___ZN10QTcpSocketD1Ev'
error: undefined reference to `_imp___ZN10QTcpSocketD1Ev'
error: collect2: ld returned 1 exit status@Any idea what's up?
-
QTcpSocket is part of the QtNetwork Module.
To include the definitions of the module's classes, use the following directive:
#include <QtNetwork>
To link against the module, add this line to your qmake .pro file:
QT += network
-
Just for you to understand your mistake, when you're writing :
@
QTcpSocket messenger();
@You're not declaring a local messenger variable and calling its constructor (QTcpSocket constructor), but you're in fact declaring a function that takes no parameter and returns a QTcpSocket.
That's why you get a compiler error. That's a common mistake.
-
To make it properly work, you will need to host something like a server on the other machines to listen for the connection and simply reply. So you'll know when they are connected or not.
To figure out the ping, you may measure the time between the send and receive packets. -
so-called "C++ most vexing parse" by Scott Meyers :)
[quote author="octal" date="1314905127"]Just for you to understand your mistake, when you're writing :
@
QTcpSocket messenger();
@You're not declaring a local messenger variable and calling its constructor (QTcpSocket constructor), but you're in fact declaring a function that takes no parameter and returns a QTcpSocket.
That's why you get a compiler error. That's a common mistake.[/quote]
-
[quote author="AlekseyK" date="1388806544"]@messenger.connectToHost("192.168.0.254", 61000);
if(!messenger.waitForConnected(3000))@
this works only if we know an open port on a host. If not how can we ping then?[/quote]
this is exactly what is "ping" for...
Either way you implement the ping protocol by yourself, r call the systems ping-application and parse the response, etc. if you need it, or use a C++ ping lib, don't know if there is any available, just google it.