Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Ping-like Qt App - QTcpSocket?
QtWS25 Last Chance

[Solved] Ping-like Qt App - QTcpSocket?

Scheduled Pinned Locked Moved General and Desktop
12 Posts 8 Posters 18.7k Views
  • 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 Offline
    P Offline
    phlucious
    wrote on last edited by
    #1

    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
    0
    • R Offline
      R Offline
      Richard Neal
      wrote on last edited by
      #2

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

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

      1 Reply Last reply
      0
      • P Offline
        P Offline
        phlucious
        wrote on last edited by
        #3

        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
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • R Offline
            R Offline
            Richard Neal
            wrote on last edited by
            #5

            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
            0
            • O Offline
              O Offline
              octal
              wrote on last edited by
              #6

              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
              0
              • C Offline
                C Offline
                Cayan
                wrote on last edited by
                #7

                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
                0
                • P Offline
                  P Offline
                  phlucious
                  wrote on last edited by
                  #8

                  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
                  0
                  • C Offline
                    C Offline
                    Cayan
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      AlekseyK
                      wrote on last edited by
                      #10

                      @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
                      0
                      • E Offline
                        E Offline
                        Exec
                        wrote on last edited by
                        #11

                        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
                        0
                        • raven-worxR Offline
                          raven-worxR Offline
                          raven-worx
                          Moderators
                          wrote on last edited by
                          #12

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

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved