[Solved] Ping-like Qt App - QTcpSocket?
-
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?
-
Add the network module to your .pro file
@
Qt += network
@
and be sure to re-run qmake. -
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. -
Thanks for the feedback! Adding
@QT += network@
to the *.pro file did the trick.Cayan, I see what you're saying about needing a server, but that's another problem for another time. :) Thanks!
-
Sure, just tried to complement the full spam of replies with the same answer
-
@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? -
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.