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]How to check socket connect to host
Qt 6.11 is out! See what's new in the release blog

[Solved]How to check socket connect to host

Scheduled Pinned Locked Moved General and Desktop
12 Posts 4 Posters 13.0k Views 1 Watching
  • 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.
  • p3c0P Offline
    p3c0P Offline
    p3c0
    Moderators
    wrote on last edited by
    #2

    Hi,

    If the connection succeeds "connected":https://qt-project.org/doc/qt-5/qabstractsocket.html#connected signal is emitted.
    You can connect this signal to your slot.

    157

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DBoosalis
      wrote on last edited by
      #3

      also can check by

      @if (socket->state() == QAbstractSocket::Connected)@

      1 Reply Last reply
      0
      • A Offline
        A Offline
        anmol2701
        wrote on last edited by
        #4

        Thanks for the reply..

        I am using
        if(socket->state() == QAbstractSocket::ConnectedState)
        to check but it is not working..

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DBoosalis
          wrote on last edited by
          #5

          What is the state value returned ?

          1 Reply Last reply
          0
          • A Offline
            A Offline
            anmol2701
            wrote on last edited by
            #6

            if i use

            if(socket->state() == QAbstractSocket::ConnectedState || QAbstractSocket::ConnectingState)

            it always returns true

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DBoosalis
              wrote on last edited by
              #7

              Of course the statement below is always true:

              @if(socket->state() == QAbstractSocket::ConnectedState || QAbstractSocket::ConnectingState)
              @

              as QAbstractSocket::ConnectingState is certainly greater then zero.

              You need to change the statement to

              @if((socket->state() == QAbstractSocket::ConnectedState) ||
              (socket->state() == QAbstractSocket::ConnectingState))

              @

              1 Reply Last reply
              0
              • A Offline
                A Offline
                anmol2701
                wrote on last edited by
                #8

                Thanks all for the reply..

                First of all i am checking socket is connected using

                  if (socket->state() == QAbstractSocket::UnconnectedState)
                

                it is working fine, then i add

                  socket->connectToHost(server->text(), port->value());
                

                after this i want to know is it conncet or not??

                Moreover, I try to use this

                   if (socket->state() == QAbstractSocket::connectedState)
                

                But it is not working?

                Could you please guide me?

                1 Reply Last reply
                0
                • p3c0P Offline
                  p3c0P Offline
                  p3c0
                  Moderators
                  wrote on last edited by
                  #9

                  Did you try connecting to the "connected":https://qt-project.org/doc/qt-5/qabstractsocket.html#connected signal ?

                  157

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    DBoosalis
                    wrote on last edited by
                    #10

                    Look at the example in the documentation (fortune client)
                    "http://qt-project.org/doc/qt-5/qtnetwork-fortuneclient-example.html":http://qt-project.org/doc/qt-5/qtnetwork-fortuneclient-example.html.

                    If you still have trouble post your code

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      anmol2701
                      wrote on last edited by
                      #11

                      Thanks!
                      It works..
                      I simply check it using condition
                      if(!socket->waitForConnected(1000))
                      {
                      //code
                      }

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        ankursaxena
                        wrote on last edited by
                        #12

                        If your problem is solved then please prepend your title as <solved>.

                        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