Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [Solved] Ping-like Qt App - QTcpSocket?

    General and Desktop
    8
    12
    17998
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      phlucious last edited by

      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!

      1 Reply Last reply Reply Quote 0
      • R
        Richard Neal last edited by

        Remove the () from messenger.
        @
        TcpSocket messenger; // !messenger()@

        For every positive is a negative and every negative is a positive.

        1 Reply Last reply Reply Quote 0
        • P
          phlucious last edited by

          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?

          1 Reply Last reply Reply Quote 0
          • L
            lgeyer last edited by

            Add the network module to your .pro file
            @
            Qt += network
            @
            and be sure to re-run qmake.

            1 Reply Last reply Reply Quote 0
            • R
              Richard Neal last edited by

              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

              For every positive is a negative and every negative is a positive.

              1 Reply Last reply Reply Quote 0
              • O
                octal last edited by

                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.

                1 Reply Last reply Reply Quote 0
                • C
                  Cayan last edited by

                  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.

                  1 Reply Last reply Reply Quote 0
                  • P
                    phlucious last edited by

                    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!

                    1 Reply Last reply Reply Quote 0
                    • C
                      Cayan last edited by

                      Sure, just tried to complement the full spam of replies with the same answer

                      1 Reply Last reply Reply Quote 0
                      • A
                        AlekseyK last edited by

                        @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?

                        1 Reply Last reply Reply Quote 0
                        • E
                          Exec last edited by

                          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]

                          Knowledge is power!

                          1 Reply Last reply Reply Quote 0
                          • raven-worx
                            raven-worx Moderators last edited by

                            [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.

                            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                            If you have a question please use the forum so others can benefit from the solution in the future

                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post